Forum Moderators: phranque

Message Too Old, No Replies

Concerned about .htaccess rewrite loops or conflicts.

         

Linda_A

9:54 pm on Apr 20, 2006 (gmt 0)

10+ Year Member



I am wondering if anyone could take a look at the rewrite rules that I currently use, to see if there's any risk of them causing conflicts or loops.


RewriteEngine on

RewriteCond %{REQUEST_URI} ^/Shared/ [OR]
RewriteCond %{REQUEST_URI} ^/Westeros/[OR]
RewriteCond %{REQUEST_URI} ^/Index [OR]
RewriteCond %{REQUEST_URI} ^/Digest[OR]
RewriteCond %{REQUEST_URI} ^/Updates
RewriteRule ^([^/]+.*)$ /index.php/$1[L]

RewriteCond %{REQUEST_URI} ^/About [OR]
RewriteCond %{REQUEST_URI} ^/Affilations [OR]
RewriteCond %{REQUEST_URI} ^/Feedback
RewriteRule ^([^/]+.*)$ /Index/$1[L]

RewriteCond %{REQUEST_URI} ^/ASoWS/Calendar[OR]
RewriteCond %{REQUEST_URI} ^/ASoWS/Gallery[OR]
RewriteCond %{REQUEST_URI} ^/ASoWS/News[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Announcements[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Calendar[OR]
RewriteCond %{REQUEST_URI} ^/BoD/CDB[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Chronicle[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Gallery[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Houses[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Logs[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Tidings[OR]
RewriteCond %{REQUEST_URI} ^/BoD/Transcripts[OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Index[OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Characters[OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Encyclopaedia[OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Heraldry[OR]
RewriteCond %{REQUEST_URI} ^/Citadel/SSM
RewriteRule ^([^/]+)/([^/]+.*)$ /$1/index.php/$2[L]

RewriteCond %{REQUEST_URI} ^/Citadel/About [OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Affilations [OR]
RewriteCond %{REQUEST_URI} ^/Citadel/Feedback
RewriteRule ^([^/]+)/([^/]+.*)$ /$1/Index/$2[L]

RewriteCond $1 ^(.*)RSS10(.*)
RewriteRule ^(.*) %1RSS20%2[NC,L]

The purpose of these rewrites is to clean up the URLs produced by the CMS I use. So, it allows me to use http://example.com/Updates/ instead of http://example.com/index.php/Updates/, and http://example.com/Citadel/About/ instead of http://example.com/Citadel/index.php/Index/About/.

In most cases, it is only necessary to remove index.php, but in some cases its index.php/Index that needs to be removed, so additional rules have been added for that, and I am wondering if those might cause any problems.

[edited by: jdMorgan at 1:31 am (utc) on April 21, 2006]
[edit reason] Example.com [/edit]

jdMorgan

1:28 am on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tested this code and encountered a problem?

Doing extensive code reviews for a single site is a bit beyond the usual scope of this forum, but generally, you must exclude the destination URL of a rewrite from being rewritten itself, if that destination URL will match the pattern of the RewriteRule.

As a single simple example, your first rule


RewriteRule ^([^/]+.*)$ /index.php/$1 [L]

could loop because the URL-path you are rewriting to, "index.php/<something>", would match the pattern ^([^/]+.*)$ of the rule itself, which means "one or more characters not equal to a slash, followed by any number of any characters" (which could include a slash and/or anything else).

The cure for this situation is to explicitly exclude URLs starting with "/index.php/" from being rewritten by adding


RewriteCond %{REQUEST_URI} !^/index\.php

ahead of the RewriteRule.

This RewriteCond requires that the requested URL not start with "/index.php" before the following RewriteRule can be invoked.

The reason this is needed is that in an .htaccess context, any rewrite which is invoked will trigger a restart of mod_rewrite and mod_access processing. This is necessary to prevent .htaccess files (considered to be "user-level" config files) from being used to by-pass server-level access controls and further access controls implemented in higher-directory-level and peer--directory-level .htaccess files. Therefore, any RewriteRule invoked in an .htaccess file will cause the entire .htaccess file to be reprocessed by mod_rewrite in the context of the current HTTP request, making it seem to be recursively calling itself.

Jim

Linda_A

10:33 am on Apr 21, 2006 (gmt 0)

10+ Year Member




Have you tested this code and encountered a problem?

The rewrites as such work as they should, but I have encountered some odd errors on my site of late. I have no idea if they're .htaccess related, but I am investigating every angle.

I know nothing of regexps myself (the code was written for me by someone else), so I am unable to determine for myself whether anything is causing any loops or not. I've added the rule you suggested, though. However, you give this as the reason for adding that bit:


The cure for this situation is to explicitly exclude URLs starting with "/index.php/" from being rewritten by adding

I am not quite sure what situation could possibly involve an URL starting with that. Cam you give an example?

I am also wondering if 'Index' might pose a similar problem given that it too is included as part of a rewrite rule.