#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    24
    Rep Power
    0

    Rule causes infinite loop, broken behaviour plus unknown rule


    Hi,

    I've taken over a project that includes a - in my opinion - fairly complicated .htaccess file, specifically since what I'm trying to add to it doesn't work. I assume there's a conflict with another rule.

    I've pasted the complete contents of the .htaccess file below for reference. I'm sure there are ways to improve upon what has been done and I'd love to hear it / learn, but my two concrete problems is as follows:

    1) I'd like to enable trailing slashes to avoid duplicate content in google webmaster tools.

    An example of this is:

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !#
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.[DOMAIN].net/$1/ [L,R=301]
    I am however not sure where in the .htaccess to put this for it to work correctly, and I suspect it conflicts with other parts of it. The result is for example that it works in a browser like Firefox but breaks the availability of some resources on the site in Chrome (returns the user to 401.shtml) and error log shows infinite loop. I tried different places in the file with various results, but never a satisfying one. I am sure my lack of knowledge isn't helping the matter.

    2) I'm interested in knowing what
    Code:
    RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|#.*|\?.*|/[^.]*)$  [NC]
    does. It's wrapped within the part that comes default with Wordpress, although it's not a part of the initial block that Wordpress generates. It's been added later.

    Below is the full contents of the htaccess file. I've omitted the domain name as I didn't find it relevant to this question.

    Thank you so much for your help.

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^[ALTTERNATIVEDOMAIN]\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.[ALTTERNATIVEDOMAIN]\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^[ALTTERNATIVEDOMAIN]\.net$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.[ALTTERNATIVEDOMAIN]\.net$ [OR]
    RewriteCond %{HTTP_HOST} ^[ALTTERNATIVEDOMAIN]\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.[ALTTERNATIVEDOMAIN]\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^[ALTTERNATIVEDOMAIN2]\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.[ALTTERNATIVEDOMAIN2]\.org$
    RewriteRule ^(.*)$ "http\:\/\/www\.[DOMAIN]\.net\/$1" [R=301,L]
    
    RewriteRule ^home/(.*) /member.php?u=$1      [L]
    RewriteRule ^live/ /activity.php      [L]
    RewriteRule ^game/([^/]*)/?$ /games.php?g=$1 [L]
    RewriteRule ^trophies/([^/]+)(/([^/]+))/?$ /games.php?g=$1&page=trophies&senid=$2 [L]
    RewriteRule ^trophies/([^/]*)/?$ /games.php?g=$1&page=trophies [L]
    RewriteRule ^eyecandy/([^/]*)/?$ /games.php?g=$1&page=media [L]
    RewriteRule ^news/([^/]*)/?$ /games.php?g=$1&page=news [L]
    RewriteRule ^review/([^/]*)/?$ /games.php?g=$1&page=review [L]
    RewriteRule ^comments/([^/]*)/?$ /games.php?g=$1&page=discuss [L]
    RewriteRule ^roster/([^/]*)/?$ /games.php?g=$1&page=roster [L]
    RewriteRule ^trophyprofile$ /trophy.php [L]
    RewriteRule ^trophyprofile/([^/]*)/?$ /trophy.php?senid=$1 [L]
    RewriteRule ^video/([^/]+)(/([^/]+))/?$ /video.php?game=$1&id=$2 [L]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|#.*|\?.*|/[^.]*)$  [NC]
    RewriteRule . /index.php [L]
    
    </IfModule>
    
    Header unset Pragma
    FileETag None
    Header unset ETag
    
    <IfModule mod_headers.c>
      <FilesMatch "\.(js|css|xml|gz)$">
        Header append Vary Accept-Encoding
      </FilesMatch>
    </IfModule>
    
    <FilesMatch "\.(gif|jpg|jpeg|png|ico)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 month"
    </FilesMatch>
    
    <FilesMatch "\.(css|js|html?|xml|txt)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 week"
    </FilesMatch>
    
    <IfModule mod_deflate.c>
    	 AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
    	 SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf)$ no-gzip dont-vary
    	 BrowserMatch ^Mozilla/4 gzip-only-text/html
    	 BrowserMatch ^Mozilla/4\.[0678] no-gzip
    	 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    	 Header append Vary User-Agent env=!dont-vary
    </IfModule>
    
    #IndexIgnore *
    Options -Indexes
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Originally Posted by stipey
    1) I'd like to enable trailing slashes to avoid duplicate content in google webmaster tools.

    An example of this is:

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !#
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.[DOMAIN].net/$1/ [L,R=301]
    I am however not sure where in the .htaccess to put this for it to work correctly, and I suspect it conflicts with other parts of it. The result is for example that it works in a browser like Firefox but breaks the availability of some resources on the site in Chrome (returns the user to 401.shtml) and error log shows infinite loop. I tried different places in the file with various results, but never a satisfying one. I am sure my lack of knowledge isn't helping the matter.
    Put it near the top so any redirect happens as early as possible.

    There will be an infinite loop if the request has a query string, like
    Code:
    /newreply.php?do=newreply&p=2878502
    As you can see the REQUEST_URI (which includes the query string) does not end in a slash, so your redirect will send them to
    Code:
    /newreply.php/?do=newreply&p=2878502
    which still doesn't end in a slash...

    One option: move the slash check into the Rule itself.
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)[^/]$ http://www.[DOMAIN].net/$0/ [L,R=301]
    I also removed the !# Cond because anchors/fragments are never included in the request URI.

    Originally Posted by stipey
    2) I'm interested in knowing what
    Code:
    RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|#.*|\?.*|/[^.]*)$  [NC]
    does. It's wrapped within the part that comes default with Wordpress, although it's not a part of the initial block that Wordpress generates. It's been added later.
    Checks if the request URI
    * ends with a slash
    * ends with .htm, .php, or .html
    * contains a # - which it never should
    * contains a question mark - present if there's a query string
    * contains a slash that is never followed by a period
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    24
    Rep Power
    0
    Thank you for the help.

    Simply reducing the block to

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)[^/]$ http://www.[DOMAIN].net/$0/ [L,R=301]
    seemed to have helped.

    Much appreciated
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    24
    Rep Power
    0
    Actually I take that back.

    I have an admin area secure with htpasswd in the root directory (www/adminfolder)

    With this enabled in www:
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)[^/]$ http://www.[DOMAIN].net/$0/ [L,R=301]
    I can't reach the admin-folder, it simply returns a 404 (redirects to 401.shtml). If I comment it out I can reach the folder just fine.

    This was always the issue I had with the trailing slash bit as it seemed to interfer with something else in the .htaccess file I pasted above.

    If you have any idea as to why this happens I would like to hear it.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Do you have an ErrorDocument set up for 401s?

    You can also add in a
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    for kicks, but I don't expect that to fix it.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    24
    Rep Power
    0
    I do not have that set up, so not really sure what causes it but the following post might help: stackoverflow

    I tried with

    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    but no luck. Thanks for the suggestion though.
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Yeah, that would be it alright. Did the explanation of what's happening make sense?
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^([^?]*[^?/])(\?|$)
    RewriteRule ^ http://www.[DOMAIN].net%1/ [L,R=301,QSA]
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    24
    Rep Power
    0
    This seem to have solved the problem. Might be cheating, I don't know.

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/401.shtml [NC] 
    RewriteRule ^(.*)[^/]$ http://www.psmania.net/$0/ [L,R=301]
    Thanks for all the help

IMN logo majestic logo threadwatch logo seochat tools logo