2

Is it possible to turn this URL:

example.com/send.php?url=google.com&name=&submit=submit

Into this URL:

example.com/google.com

When I try I just keep getting 404 or 500 errors and it's frustrating.

Here's a few thing's I've tried.

RewriteRule ^([^/]*)$ /send.php?url=$1&name=&submit=submit [NC,L]
RewriteRule ^([-\w\.]*)$ /send.php?url=$1&name=&submit=submit [NC,L]
RewriteRule ^(.*)$ /send.php?url=$1&name=&submit=submit [NC,L]

If it's not possible then please could you tell me why it's not. I'm rather new to mod_rewrite and want to learn.

Tero Kilkanen
  • 37,584

4 Answers4

0

You can try with the below rules.

RewriteCond %{REQUEST_URI} !send.php
RewriteRule ^([-\w\.])$ /send.php?url=$1&name=&submit=submit [NC,L]
  • Thanks, this works! But with a different pattern RewriteRule ^([^/]*)$ send.php?url=$1&name=&submit=submit [NC,L] The only problem now is I can't get to the homepage. When I type example.com it doesn't go to the homepage. It stays on send.php and the REQUEST_URI is '/'. So mod rewrite just considers url= to be an empty string. – penfold_32 Oct 11 '16 at 13:51
  • You don't need to escape the dot to match a literal dot in a character class, and you don't need the NC flag here, since \w already includes upper and lowercase letters. You would also need to extend the pattern to include more characters, currently it's only matching 1 character. ie. ^([-\w.]+)$ – MrWhite Oct 11 '16 at 14:48
  • @penfold_32 Your homepage is rewritten because the pattern ^([^/]*)$ matches 0 characters (ie. the homepage, /). Change it to include 1 or more. eg. ^([^/]+)$. However, that pattern looks too generic, based on the example in your question. – MrWhite Oct 11 '16 at 14:52
0

Or you could try

RewriteRule ^(.*) /$1/? [L,R=301]

I'm no redirect expert, but I think the question mark at the end cuts off all parameters.

0

If you are not rewriting anything else just use:

FallBackResource /send.php

and parse the PATHINFO

Unbeliever
  • 2,366
  • You would need to parse the REQUEST_URI, not the PATH_INFO. (There is no additional path information on this request.) – MrWhite Oct 11 '16 at 16:06
0

If you want to parse out the parameter to use it as file/directory you should try this:

RewriteRule ^/send\.php\?url=([-\w\.])\&.*$ /$1 [NC,L]
  • You can't match the query string with the RewriteRule pattern. But it would seem the OP is wanting to "go the other way"? – MrWhite Oct 11 '16 at 14:39
  • You are right. Sorry, I forgot about this. I'm more familiar with nginx where its possible. – seobility Oct 11 '16 at 14:54