Brian Leishman
Brian Leishman

Reputation: 8555

htaccess redirect stuck in infinite recursion

I seem to have a strange problem that's only happening on one machine and not the other, the apache2 version on this one is 2.4.23 (the not working one), and on the working one I have apache version 2.4.10.

The rewrite snippet I have redirects URLs without extensions to .html internally if the file exists and looks like this

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L,QSA]

I remove this piece the redirect loops stops, but obviously my extensionless URLs no longer get redirected.

I've evolved it to this, which I thought for sure to stop the redirect problem, but still loops forever

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]

Enabling the debug log level on the apache server in question gives a ton of lines that look similar to this one

[Wed May 30 11:49:25.669176 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] applying pattern '^(.*)$' to uri '2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html'
[Wed May 30 11:49:25.669200 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] add path info postfix: /domains/domain.com/public_html/2012/06/some-blog-post -> /domains/domain.com/public_html/2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html
[Wed May 30 11:49:25.669215 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] strip per-dir prefix: /domains/domain.com/public_html/2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html -> 2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html

I'm afraid I'm not super well versed with the htaccess syntax so maybe I'm missing something obvious


Here's the entire htaccess file (sorry it's a tad lengthy)

RedirectMatch   301 ^/quote/?$              /pricing
RedirectMatch   301 ^/order\.htm/?$         /order
RedirectMatch   301 ^/contact\.htm/?$       /contact
RedirectMatch   301 ^/coin-gallery\.htm/?$  /gallery
RedirectMatch   301 ^/gallery-html/?$       /gallery

RedirectMatch   301 ^/baseball-html/?$      /baseball-trading-pins
RedirectMatch   301 ^/softball-html/?$      /baseball-trading-pins
RedirectMatch   301 ^/volleyball-html/?$    /baseball-trading-pins
RedirectMatch   301 ^/basketball-html/?$    /baseball-trading-pins
RedirectMatch   301 ^/football-html/?$      /baseball-trading-pins
RedirectMatch   301 ^/soccer-html/?$        /baseball-trading-pins
RedirectMatch   301 ^/hockey-html/?$        /baseball-trading-pins


##blogs-500s
RedirectMatch   301 ^/tag.*$        /blog
RedirectMatch   301 ^/wp.*$         /blog
RedirectMatch   301 ^/category\/.*$         /blog
RedirectMatch   301 ^/[^a-zA-Z]+$   /blog


##pins
RedirectMatch   301 ^/sitemap-html/?$                   /
RedirectMatch   301 ^/quote_lapelpins\.html/?$          /
RedirectMatch   301 ^/custom-html/?$                    /custom-lapel-pin-styles    
RedirectMatch   301 ^/softenamel-html/?$                /soft-enamel-pins   
RedirectMatch   301 ^/cardstock-html/?$                 /cardstock  
RedirectMatch   301 ^/employee-html/?$                  /employee-recognition-pins
RedirectMatch   301 ^/employee-recognition/?$           /employee-recognition-pins
RedirectMatch   301 ^/baseball-html/?$                  /baseball-trading-pins
RedirectMatch   307 ^/softball-html/?$                  /baseball-trading-pins
RedirectMatch   307 ^/volleyball-html/?$                /baseball-trading-pins
RedirectMatch   307 ^/basketball-html/?$                /baseball-trading-pins
RedirectMatch   307 ^/football-html/?$                  /baseball-trading-pins
RedirectMatch   307 ^/usssaquote_tradingpins\.html/?$   /custom-usssa-trading-pins?lid=Ij2
RedirectMatch   307 ^/soccer-html/?$                    /baseball-trading-pins
RedirectMatch   307 ^/hockey-html/?$                    /baseball-trading-pins  
RedirectMatch   307 ^/cloisonne-html/?$                 /cloisonne-pins
RedirectMatch   301 ^/pin-pricing/?$                    /pricing
RedirectMatch   301 ^/rush-pins-2/?$                    /rush-pins
RedirectMatch   301 ^/baseball\.html/?$                 /baseball-trading-pins
RedirectMatch   307 ^/softball\.html/?$                 /baseball-trading-pins  
RedirectMatch   307 ^/soccer\.html/?$                   /baseball-trading-pins
RedirectMatch   307 ^/volleyball\.html/?$               /baseball-trading-pins
RedirectMatch   307 ^/basketball\.html/?$               /baseball-trading-pins
RedirectMatch   307 ^/football\.html/?$                 /baseball-trading-pins
RedirectMatch   307 ^/hockey\.html/?$                   /baseball-trading-pins  
RedirectMatch   301 ^/contactus-html/?$                 /contact
RedirectMatch   301 ^/aboutus-html/?$                   /about-us
RedirectMatch   301 ^/aboutus\.html/?$                  /about-us
RedirectMatch   301 ^/aboutus\.htm/?$                   /about-us
RedirectMatch   301 ^/termsandconditions-html/?$        /terms
RedirectMatch   301 ^/termsandconditions\.html/?$       /terms
RedirectMatch   301 ^/privacy-htm/?$                    /privacy
RedirectMatch   301 ^/presentation\.html/?$             /presentation-options
RedirectMatch   301 ^/presentation-html/?$              /presentation-options
RedirectMatch   301 ^/attachments\.html/?$              /attachments
RedirectMatch   301 ^/attachments-html/?$               /attachments
RedirectMatch   301 ^/platingchart\.html/?$             /plating
RedirectMatch   301 ^/platingchart-html/?$              /plating
RedirectMatch   301 ^/gemstones\.html/?$                /gemstones
RedirectMatch   301 ^/trading-pin-options/?$            /baseball-trading-pins  
RedirectMatch   301 ^/trading-pin-gallery/?$            /gallery
RedirectMatch   301 ^/trading-pin-gallery-2/?$          /gallery    
RedirectMatch   301 ^/trading-pin-gallery-3/?$          /gallery
RedirectMatch   301 ^/trading-pin-gallery-4/?$          /gallery
RedirectMatch   301 ^/artwork\.html/?$                  /artwork
RedirectMatch   301 ^/artwork-html/?$                   /artwork
RedirectMatch   301 ^/keychains-html/?$                 /keychains
RedirectMatch   301 ^/challengecoins-html/?$            /custom-challenge-coins
RedirectMatch   301 ^/medallions-html/?$                /medals-medallions  
RedirectMatch   301 ^/wristbands-html/?$                /custom-wristbands
RedirectMatch   301 ^/lanyards-html/?$                  /lanyards
RedirectMatch   301 ^/quote_tradingpins\.html/?$        /custom-usssa-trading-pins?lid=Ij2
RedirectMatch   301 ^/usssa-pins/?$                     /custom-usssa-trading-pins?lid=Ij2
RedirectMatch   301 ^/contactus\.html/?$                /contact
RedirectMatch   301 ^/fraternitysorority-html/?$        /fraternity-and-sorority-pins


##coins
RedirectMatch   301 ^/coin-bottle-openers\.htm/?$   /coin-bottle-openers
RedirectMatch   301 ^/coin-gallery\.htm/?$          /gallery
RedirectMatch   301 ^/coin-gallery.*$               /gallery
RedirectMatch   301 ^/coin-pricing\.htm/?$          /pricing
RedirectMatch   301 ^/create\.htm/?$                /create
RedirectMatch   301 ^/presentation.htm/?$           /coin-presentation-options
RedirectMatch   301 ^/history\.htm/?$               /challenge-coin-history
RedirectMatch   301 ^/faq\.htm/?$                   /faq
RedirectMatch   301 ^/testimonials\.htm/?$          /testimonials
RedirectMatch   301 ^/quote/?$                      /create
RedirectMatch   301 ^/quote/view(\.php)?$           /view

##patches
RedirectMatch   301 ^/pvc-patches/?$    /pvc







<Files "header.php">
    Order Allow,Deny
    Deny from all
</Files>

<Files "footer.php">
    Order Allow,Deny
    Deny from all
</Files>
<Files "config.php">
    Order Allow,Deny
    Deny from all
</Files>

<Files "version">
    Order Allow,Deny
    Deny from all
</Files>

<Files "superheader.php">
    Order Allow,Deny
    Deny from all
</Files>

Options -Indexes

ErrorDocument 404   /404.html
ErrorDocument 500   /505.html

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ $1.php [L,QSA]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]

RewriteRule ^/?img/([A-Fa-f0-9]{64}).gif$ /scripts/trackemail.php?id=$1 [QSA,L]

EDIT 2

Whoops! I forgot the URL, that should have been first thing I listed, but the path in this example would be

/2012/06/some-blog-post/privacy


This is the apache2 response headers when that URL is loaded

HTTP/1.1 500 Internal Server Error
Date: Wed, 06 Jun 2018 14:54:45 GMT
Server: Apache
Content-Length: 666
Connection: close
Content-Type: text/html; charset=iso-8859-1

I grabbed a bit more (a ton) of the apache2 log after enabling the debug LogLevel and put it here https://pastebin.com/pu5sUJnG, adding LogLevel alert rewrite:trace3 to my apache2.conf file

Upvotes: 2

Views: 181

Answers (2)

Brian Leishman
Brian Leishman

Reputation: 8555

Another answer I seem to have stumbled upon with the Multiviews option disabled seems to be replacing all instances of %{REQUEST_FILENAME} with %{DOCUMENT_ROOT}%{REQUEST_URI} so

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ $1.php [L,QSA]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]

becomes

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

No idea why this fixes it, but it definitely appears to behave more how you would expect, not sure if that's a bug or not. Still, huge help from the author of the other answer for getting me to this point.

Upvotes: 1

anubhava
anubhava

Reputation: 785146

Have just this code in your site root .htaccess and retest with RewriteLog enabled:

Options +FollowSymLinks -Indexes +MultiViews

# commenting out for now
#ErrorDocument 404   /404.html
#ErrorDocument 500   /505.html

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{ENV:REDIRECT_STATUS} ^$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^img/([a-f0-9]{64})\.gif$ scripts/trackemail.php?id=$1 [QSA,NC,L]

Enabling MultiViews will make Apace serve correct matching .php or .html files. See http://httpd.apache.org/docs/2.4/content-negotiation.html

Upvotes: 2

Related Questions