1

I have several URLs like this:

http://site.com/knowndir/../somedir/page.html

I need to redirect them to

http://site.com/somedir/page.html

Basically, I need to remove the knowndir/../ particle from the URL. I know the knowndir part.

So far, I've tried with this rule:

RewriteRule knowndir\/\.\.\/(.*)$ http://site.com/$1 [R=301,L] but without any luck.

What am I doing wrong?

Silviu G
  • 1,241
  • 10
  • 31

1 Answers1

1

It seems, that the client already removes the . and .. path elements. You can see the difference in the Apache access.log, when you request a page with firefox or wget vs requesting a URL with telnet and GET /knowndir/../somedir/page.html HTTP/1.0.

When, despite that, Apache receives a request with . and .. path elements, Apache already takes care of that by itself, without the need for any redirect rules. This means it resolves these segments by removing . or subdir/.., in order to get an unambiguous path.

So, in the end your RewriteRule never sees knowndir/../ and thus will not send a 301 redirect.

Update:

I finally found the relevant RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax, which describes in 5.2.4. Remove Dot Segments how . and .. should be handled.

OT: Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • @SilviuG What do you see int the browser's URL bar? In my test environment this works properly. – Olaf Dietsche Feb 15 '13 at 17:45
  • In the address bar I see the right URL: `http://site.com/somedir/page.html`. However, when testing the URLs with SamSpade I get a `200` response code and the page in question. **Note:** the right URL was shown by my browser (Firefox) even before I attempted the redirects. It seems that the browser understands the URL and corrects it. – Silviu G Feb 18 '13 at 09:27
  • @SilviuG Which response do you expect and why? – Olaf Dietsche Feb 18 '13 at 09:39
  • I expect a `301` and a `Location: ` header – Silviu G Feb 18 '13 at 09:42
  • Hmm... the question was asked because I have some `/knowndir/../somedir/page.html` pages in GWT for a site (composed of a bunch of HTML files, no CMS), and I was looking to do a 301 redirect for those wrong links (all the links are relative, that's why the `../` is used), and I don't know how to search for them. Thanks anyway. – Silviu G Feb 19 '13 at 07:04
  • @SilviuG If the client already removes `..`, there's no way to find them. But if there are clients wich don't remove them (I tested with firefox and wget only), you can search through your log files for `..`. Maybe you will find some requests there. – Olaf Dietsche Feb 19 '13 at 08:07