Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Possible Loop

RewriteRule Possible Loop

         

harad

9:03 pm on Dec 2, 2008 (gmt 0)

10+ Year Member



Hi All

I have been told the following rule could possibly causing a looping error on our site and I was just wondering the best way to correct it.

My knowledge of RewriteRules is nil so any help would be very much appreciated.

-------------------------

RewriteRule ^adverts/(.*) adverts/$1 [L,PT]

-------------------------

Cheers

Harad

g1smd

12:17 am on Dec 3, 2008 (gmt 0)

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



"Add a RewriteCond that checks THE_REQUEST", is the usual answer to this question. Then it will only rewrite once.

However on this occasion, I get to ask the questions: What is that line of code actually supposed to do?

harad

10:09 am on Dec 3, 2008 (gmt 0)

10+ Year Member



Hi and thanks

Here is the entire .htaccess file. This line is to allow another program in the adverts directory to function correctly apparently. Any help or advice would be great.

Options +FollowSymLinks
Redirect 301 /testing301.php [domainname.com...]
RewriteEngine on
RewriteRule ^sitemap.xml index.php?app=$1&ns=googlesitemap [L,PT]
RewriteRule ^apc.php apc.php [L,PT]
RewriteRule ^admin.php admin.php [L,PT]
RewriteRule ^index.php index.php [L,PT]
RewriteRule ^installer.php installer.php [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]
RewriteRule ^relay/(.*) relay/$1 [L,PT]
RewriteRule ^adverts/(.*) adverts/$1 [L,PT]
RewriteRule ^(.*)-(.*)/(.*)\.html index.php?app=$1&ns=$2&ref=$3 [L,PT]
RewriteRule ^(.*)\.php index.php?app=gbu0&ns=catprodshow&ref=$1 [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]
rewritecond %{http_host} ^domainname.com
rewriteRule ^(.*) [domainname.com...] [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.html$ [domainname.com$1...] [R=301,L]
<FilesMatch "\.(jpg¦jpeg¦png¦gif¦swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

g1smd

8:25 pm on Dec 3, 2008 (gmt 0)

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



The index redirect needs to be listed before the non-www redirect.

All of the rewrites need to be listed so that they are after all of the redirects.

The rewrites are the things with [L] and the redirects are the things with [R=301,L]

Add a comment to each rule block to explain what the next line does.

harad

9:10 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



Thx .. is this what you mean? I wish I could add comments but I am not sure what the lines do myself.. they have been added by the software providers

Options +FollowSymLinks
Redirect 301 /testing301.php [domainname.com...]
RewriteEngine on
RewriteRule ^(.*)index\.html$ [domainname.com$1...] [R=301,L]
rewriteRule ^(.*) [domainname.com...] [R=301,L]
RewriteRule ^index.php index.php [L,PT]
RewriteRule ^(.*)\.php index.php?app=gbu0&ns=catprodshow&ref=$1 [L,PT]
RewriteRule ^(.*)-(.*)/(.*)\.html index.php?app=$1&ns=$2&ref=$3 [L,PT]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^sitemap.xml index.php?app=$1&ns=googlesitemap [L,PT]
RewriteRule ^apc.php apc.php [L,PT]
RewriteRule ^admin.php admin.php [L,PT]
RewriteRule ^installer.php installer.php [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]
RewriteRule ^relay/(.*) relay/$1 [L,PT]
RewriteRule ^adverts/(.*) adverts/$1 [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]
rewritecond %{http_host} ^domainname.com
<FilesMatch "\.(jpg¦jpeg¦png¦gif¦swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

g1smd

9:18 pm on Dec 3, 2008 (gmt 0)

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



Stop.

You have "lost" the preceding RewriteCond lines that are supposed to be associated with some of those rules.

harad

9:46 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



Ok.. this better?

Options +FollowSymLinks
Redirect 301 /testing301.php [domainname.com...]
RewriteEngine on

rewritecond %{http_host} ^domainname.com
rewriteRule ^(.*) [domainname.com...] [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.html$ [domainname.com$1...] [R=301,L]

RewriteRule ^(.*)-(.*)/(.*)\.html index.php?app=$1&ns=$2&ref=$3 [L,PT]
RewriteRule ^(.*)\.php index.php?app=gbu0&ns=catprodshow&ref=$1 [L,PT]
RewriteRule ^index.php index.php [L,PT]
RewriteRule ^sitemap.xml index.php?app=$1&ns=googlesitemap [L,PT]
RewriteRule ^installer.php installer.php [L,PT]
RewriteRule ^apc.php apc.php [L,PT]
RewriteRule ^admin.php admin.php [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]
RewriteRule ^relay/(.*) relay/$1 [L,PT]
RewriteRule ^adverts/(.*) adverts/$1 [L,PT]
RewriteRule ^news/(.*) news/$1 [L,PT]

<FilesMatch "\.(jpg¦jpeg¦png¦gif¦swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

g1smd

10:12 pm on Dec 3, 2008 (gmt 0)

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



The index redirect needs to be listed before the non-www redirect.

See earlier discussions about

(.*)
being brutally inefficient when there are multiple instances in the same rule.
This part
(.*)-(.*)/(.*)
needs to be changed.

jdMorgan

1:47 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In all cases where you rewrite ^x/(.*) to x/$1, the effect is to leave the URL-path unchanged, leave it in URL format rather than converting it to a filepath, and then to stop processing rewriterules. So the practical effect is that the subsequent rewriterules are not executed if a URL-path match is made by one of these rules; they function as an "If the URL starts with "x/", then skip the rest of the following rules" rule.

So, such rules are usually not useful unless you want to avoid executing a rule that follows one of these rewrite-to-self-and-quit rules.

However, in the new version of the rules, I don't see any RewriteRules following these self-rewriting rules, so I'm not sure what the point of them is.

I suspect that they were intended to avoid processing this rule for any of the subdirectories named in the self-rewriting rules:
RewriteRule ^(.*)\.php index.php?app=gbu0&ns=catprodshow&ref=$1 [L,PT]
but I cannot be sure.

For example, if "/admin.php" is requested, then DO NOT rewrite that to index.php?app=gbu0&ns=catprodshow&ref=admin

I can't be sure, though...

That rule is one that looks like it will loop, rewriting a request for x.php to /index.php?app=gbu0&ns=catprodshow&ref=x, and then rewriting that to /index.php?app=gbu0&ns=catprodshow&ref=index, and then repeatedly rewriting that URL to itself until the server's maximum internal redirection limit is reached (see your server error log).

Any rule in .htaccess that produces an output that matches its own input pattern must have a RewriteCond that excepts its own output URL-path from being rewritten. For example:


RewriteCond $1 !^index$
RewriteRule ^(.*)\.php index.php?app=gbu0&ns=catprodshow&ref=$1 [L,PT]

As is typical with rules provided with 3rd-party software, those rewrite-to-self-and-quit rules are not well-written, since each could be coded as

RewriteRule ^x/(.*)$ - [PT,L]

thus saving some typing, CPU resources, and potential errors.

Basically, we can read the code and understand what it does to URLs, line-by-line. But we cannot tell what it was intended to accomplish, or what its practical effect is on your site, without fully-understanding what all those subdirectories are, where your scripts are located, and a bunch of other details.

You might want to call in a local contractor to take a few days to go over all of this with you, and make it clear that he/she won't be paid unless the resulting config file is fully-documented with clear, correct, and concise-but-complete comments preceding every line of code and every block of code... :)

Jim