Forum Moderators: phranque

Message Too Old, No Replies

Period generated 404 in mod_rewrited URL.

         

Jesse_Smith

1:18 pm on Oct 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do I allow a period in the changed part of the URL?

I got

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^directory/([^.]+)$ directory/index.php?title=$1 [L]

And
domain.com/directory/Widget_101
works, but
domain.com/directory/Widget.101
doesn't.

It's a wiki script, so I can't keep visitors from having files created with periods!

RewriteRule ^directory/([^.]+)\.([^.]+)\.([^.]+)$ directory/index.php?title=$1.$2.$3 [L]
RewriteRule ^directory/([^.]+)\.([^.]+)$ directory/index.php?title=$1.$2 [L]

doesn't fix it. Server tries to redirect over and over until it gives up.

RewriteRule ^directory/(.*)$ directory/index.php?title=$1 [L]

also doesn't work, even with out the period. It also tries to redirect until it gives up.

jdMorgan

2:27 pm on Oct 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure I understand the issue with the period, but notwithstanding that, you'll have problems with any rewrite where the new URL matches the RewriteRule's pattern. This is because mod_rewrite in an .htaccess context is effectively recursive -- it behaves as if it calls itself after any rewrite is invoked.

This is because, for security and other reasons, it is necessary to re-start the configuration code on the server after any rewrite, so that the newly-rewritten URL can be tested against access controls such as hotlink protection and password protection, other "rewrites" such as ScriptAliases, etc.

The simple solution is to exclude the new URL from being re-rewritten by specifically testing for it before the rewrite.

Taking your second rule as an example, the problem is that "directory/index.php?<any query> matches the pattern "^directory/([^.]+)\.([^.]+)$" and so will be rewritten recursively. So you need to prevent that from happening:


RewriteCond %{REQUEST_URI} !^/directory/index\.php
RewriteRule ^directory/([^.]+)\.([^.]+)$ directory/index.php?title=$1.$2 [L]

An alternative method useful in some circumstances is to test the value of the QUERY_STRING variable. Note that the query string *is not* visible to or testable by RewriteRule, and must be tested with a RewriteCond %{QUERY_STRING} construct. This is because query strings are not considered to be part of a URL, but rather, data appended to a URL to be passed to the resource at that URL.

Jim

Jesse_Smith

3:45 pm on Oct 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks! That worked.

It did mess up the style sheet stuff, but moving the css files out of that directory fixed the style sheet.