Forum Moderators: phranque

Message Too Old, No Replies

How to avoid convert double slashes to single slashes?

Apache is converting double slashes to single slashes. How to avoid it?

         

MindStorm

7:00 pm on Aug 25, 2009 (gmt 0)

10+ Year Member



Hi,

I'm trying to make links like this on

http://www.domain.com/section/action/url=http://www.google.com

and I put the following line in the .htacces:

RewriteRule ^section/action/url=(.*)$ external.php?u=$2 [L,QSA]

But, in PHP, I get $_GET['url'] = "http:/www.google.com" (note that address has a single slash)

If I try:

http://www.domain.com/external.php?url=http://www.google.com

is working fine.

How can I avoid this behaviour? Or what I need to configure?

I need this to open a page with the url in a iframe.

I hope that you can help me with this issue. Thanks.

g1smd

7:32 pm on Aug 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why do you need the http:// in the parameter?

Why not just have the script add it each time?

MindStorm

10:14 pm on Aug 25, 2009 (gmt 0)

10+ Year Member



Because it can be "http://" or "https://". The addresses are previously stored in database.

Note:
In the address that I supplied to you, the uri fragments "section" and "action" are only an examples

I only just want to know how to get the correct string value of a URL (or even another value with "//") passed via Query String like in the example.

MindStorm

10:24 pm on Aug 25, 2009 (gmt 0)

10+ Year Member



This a example about what I need. You can see that google has urls in theirs links when you search for images. Example (sorry if is too large):

[images.google.com.mx...]

jdMorgan

1:59 am on Aug 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be Apache removing the double-slash, which it does do in "normal" URL-paths. In other words, if you request
example.com//a//b//c//foo.html
from a non-customized Apache server, it will reduce all those double slashes to single slashes, and happily serve a file from <DocumentRoot>/a/b/c/foo.html with no redirects or error warnings.

A few things to try:
1) Try using the [NE] flag on your rule.

2) Encode the slashes as %2f%2f, *and* use the [NE] flag on your rule to prevent re-encoding (it would end up as %252f%252f otherwise)

3) Try publishing the URL (on the page generated by your script) in encoded form -- i.e. use "http:%2f%2fgoogle.com"

4) Just use a query string to pass that URL from the script, and don't bother trying to make it look like a static URL (it doesn't look like a normal URL anyway with "http" or "https" and a domain name on the end).

Jim

MindStorm

4:17 pm on Aug 26, 2009 (gmt 0)

10+ Year Member



Thanks for your response, Jim.

I'm managing the server and the server can be customize, so i'm not worried about the double slashes in the proyect. Also, I'm trying to make friendlies URLs with the .htaccess file.

I'll consider your suggestions, but ideally for my job is to try to customize the apache server o the .htaccess file to accept "//" in query string. I know that is possible (The google example), the question is "How to?".

g1smd

10:59 pm on Aug 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be aware that if a "page" is indexed both with and without the double slash, that this is a classic example of a Duplicate Content problem.

MindStorm

3:53 am on Aug 27, 2009 (gmt 0)

10+ Year Member



@g1smd: Thanks, I'm aware about it.

I'm still searching how to done it.

jdMorgan

12:40 pm on Aug 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a basic Apache function: Consecutive slashes in URL-paths are reduced to single slashes. You will either have to modify your script to 'rebuild' double slashes following "http:" or "https:" and preceding valid domain-format strings, or you will have to download the Apache source code, modify it, and re-compila a custom Apache install. You will also have to re-do this every time Apache releases an upgrade.

Therefore, I strongly suggest revisiting the suggestions I made above.

Jim

MindStorm

4:17 pm on Aug 27, 2009 (gmt 0)

10+ Year Member



@jdMorgan: Thanks for your explanation, in some way this is the reply that i wanted. So, it's seems that is not configurable to treat double slashes in Apache. I suppose for security reasons.

At the end, I'm trying one of your suggestion: Encoding the url in the RFC 1738 standard format. (using %hex). Example

http://www.domain.com/section/action/http%3A%2F%2Fwww.google.com%2F

I'm trying to capture the url in .htaccess:

RewriteRule ^/section/section/(.*)$ externo.php?u=$2 [QSA,L]

But the server response to me with "Object not found!". I tried again by putting "?u=" (in .htaccess and in the url):

RewriteRule ^/section/section/?u=(.*)$ externo.php?u=$2 [QSA,L]

But it's seems that just is ignoring.

The rule is written correctly or I missed something?

g1smd

4:28 pm on Aug 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you use ?u= in the URL, then you'll need a RewriteCond to look at QUERY_STRING.

The other problem is that you're storing the data in $1 but then looking for it in $2 and it isn't there.

jdMorgan

4:39 pm on Aug 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In .htaccess, the URL-path examined by RewriteRule will not start with a slash, so ^/section/... will never match. Try just ^section/...

Also, if you want to pass encoded strings, you may need to use the [NE] flag on your rule.

It should not be necessary for your rule to examine the query string at this point. It may be needed later to refine the function, but not to test whether your hex-encoded URLs can pass through mod_rewrite and be un-encoded successfully by your script.

Try using the "Live HTTP Headers" add-on for Firefox/Mozilla browsers to examine the HTTP transactions between your browser and your server. This may help you to make some sense about what is going on.

Jim

MindStorm

3:38 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



Thanks g1smd, jdMorgan.

@g1smd: You are right. It must be $1 instead of $2. I mistype in the example. I'm studying about ReWriteCond.

@jdMorgan: You are right too. I tried without the starting slashes, but the same problems. I'm studying about flags too. I'm using "httpFox" addon for FireFox, but I'm trying the add-on that you suggested. Both are very useful.