1

I want to redirect a whole url to a query parameter with a RewriteRule in .htaccess

for example: http://server.com/http://google.com should be redirected to

http://server.com/index.php?url=http://google.com

so far i'm just able to make this work: http://server.com/google.com but when a : or / is contained, it doesn't work..

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=$1 [L,NC,QSA]

thanks for help!

2
  • http://server.com/http://google.com is not a valid URL, so won't ever get to the host to be rewritten.
    – user488187
    Mar 12, 2015 at 21:14
  • Although you're not asking about it specifically, I believe using URL encoding (which you don't appear to be doing, in the above example) is at least a piece of the puzzle you're trying to solve.
    – Jordan
    Mar 12, 2015 at 22:01

2 Answers 2

1

RewriteRule patter strips multiple / into one, better use RewriteCond here:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(.+)$ 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php?url=%1 [L,NC,QSA]
2
  • Thank you, this worked for me. But then it's not possible to work with relative paths anymore! So only absolute paths with this solution
    – arubacao
    Mar 12, 2015 at 22:08
  • Any path segment can be captured this way but just remember %{REQUEST_URI} always represents full path.
    – anubhava
    Mar 12, 2015 at 22:14
0

Why not just do the TLD and then add the http:// in the rule. This is how I would do it.

This is the way I would use it so it doesn't "look" invalid. http://server.com/google.com

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=http://$1 [L,NC,QSA]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.