代码之家  ›  专栏  ›  技术社区  ›  Brian Leishman

htaccess重定向陷入无限递归

  •  2
  • Brian Leishman  · 技术社区  · 6 年前

    我似乎遇到了一个奇怪的问题,它只发生在一台机器上,而不是另一台机器上,这台机器上的Apache2版本是2.4.23(不工作的版本),而在工作的机器上,我有Apache2.4.10版本。

    我所拥有的重写片段将没有扩展名的url重定向到.html(如果文件存在并如下所示)

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

    我删除了这一部分,重定向循环停止,但显然我的无扩展url不再被重定向。

    我已经把它发展到了这个阶段,我认为这肯定会阻止重定向问题,但仍然会永远循环

    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
    

    在启用调试日志级别之后,我获取了更多的Apache2日志,并将其放在这里 https://pastebin.com/pu5sUJnG ,添加 LogLevel alert rewrite:trace3 到我的apache2.conf文件

    2 回复  |  直到 6 年前
        1
  •  2
  •   anubhava    6 年前

    只是这个密码 在您的站点根目录中。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

        2
  •  1
  •   Brian Leishman    6 年前

    在禁用多视图选项的情况下,我偶然发现的另一个答案似乎是替换 %{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]
    

    不知道为什么会这样做,但它的行为显然更符合您的预期,不确定这是否是一个bug。不过,另一个答案的作者给了我很大的帮助。