htaccess重定向卡在无限递归中

时间:2018-05-30 16:33:12

标签: apache .htaccess mod-rewrite

我似乎有一个奇怪的问题,只发生在一台机器而不是另一台机器上,这个上的apache2版本是2.4.23(不能正常工作),而在工作版本上我有apache版本2.4.10

重写代码片段如果文件存在并且看起来像这样,我已将内部重定向的URL重定向到.html

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

我删除此部分重定向循环停止,但显然我的无扩展名网址不再被重定向。

我已经将它演变为这个,我认为肯定会停止重定向问题,但仍然会永远循环

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

在相关的apache服务器上启用调试日志级别会产生大量与此类似的行

[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

我担心我不是非常精通htaccess语法,所以也许我错过了一些明显的东西

这是整个htaccess文件(对不起,它有点冗长)

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]

编辑2

糟糕!我忘记了URL,这应该是我列出的第一件事,但这个例子中的路径是

  

/ 2012/06 /一些-博客-交/隐私

这是加载该URL时的apache2响应标头

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

在启用调试LogLevel并将其放在https://pastebin.com/pu5sUJnG之后,我抓了更多(一吨)的apache2日志,将LogLevel alert rewrite:trace3添加到我的apache2.conf文件中

2 个答案:

答案 0 :(得分:2)

在您的网站root .htaccess中仅此代码并在启用RewriteLog的情况下重新测试:

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]

启用MultiViews将使Apace服务正确匹配.php.html个文件。见http://httpd.apache.org/docs/2.4/content-negotiation.html

答案 1 :(得分:1)

我似乎在多视图选项被禁用时偶然发现的另一个答案似乎是用%{REQUEST_FILENAME}替换%{DOCUMENT_ROOT}%{REQUEST_URI}的所有实例,所以

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]

成为

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]

不知道为什么会修复它,但它似乎表现得更像你期望的,不确定这是不是一个错误。尽管如此,另一个答案的作者给予了我很大的帮助。