1

I have created a .htaccess file in which I try to rewrite certain URL's. My goals is to pass an URL to another script as a parameter. It kind of works, but I end up with URL's that only contain one forward slash in the protocol part (e.g. http:/ instead of http://)

This is my .htaccess file

DirectoryIndex index.php

php_flag register_globals off
php_flag magic_quotes_gpc off
php_value display_errors 0

FileETag none
ServerSignature Off

Options All -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^test/(.*)$ test.php?url=$1 [QSA,NC,NE,L]
</IfModule>

Now when I request the following : http://example.org/test/http://myurl.com, the php $_REQUEST object contains an url variable containing http:/myurl.com

EDIT When I request : http://example.org/test/http%3A%2F%2Fmyurl.com, the URL doesn't get rewritten at all (doesn't match the rewrite regex somehow)

I cannot seem to find the proper solution for this problem, it doesn't seem to be a problem with escape characters (since that would be a backslash)

2 Answers 2

2

Actually you shouldn't capture the URL from RewriteRule since Apache replaces all // by /.

Replace your rule with this:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+test/([^\s]+) [NC]
    RewriteRule ^.*$ test.php?url=%1 [QSA,NE,L]
</IfModule>

THE_REQUEST` variable represents original request received by Apache from your browser.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, you've gotten me on the right track! I had to change a few things in your answer, but you have definitely pointed me in the right direction!
Could you change the '$1' backreference to '%1' and the RewriteRule line should start with ^.*$ Then I can accept your submission as an answer.
@QNimbus Sorry I meant %1 and by mistake typed $1 corrected now.
About RewriteRule ^ it is same as RewriteRule ^.*$ (and probably faster since it doesn't match full URI again.
@ anubhava You're right about RewriteRule ^, I didn't know that! Thanks again!
0

Your problem is that in a URL the slash is considered a directory. Resolving path//file you get path/file. That's what happens in your case.

To properly encode it, you must replace the slashes of the http://myurl.com with %2F: http:%2F%2Fmyurl.com. See percent encoding for more information.

A simpler workaround would be to ditch http:// in general.

1 Comment

I understand your point, however when I escape (urlencode) the url that I want to pass, the rewrite regex doesn't match anymore somehow.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.