【问题标题】:redirect loop with mod_rewrite使用 mod_rewrite 重定向循环
【发布时间】:2021-05-21 14:20:15
【问题描述】:

我正在尝试通过 mod_rewrite 为我的页面添加语言前缀 例如:

https://example.com/ > https://example.com/en/

在内部它需要翻译成`https://example.com/?language=en 我已经设法为基本 url 做到这一点,但是当我尝试使用其他一些隐含的重定向来做到这一点时,它总是以无限循环结束。 例如:

https://example.com/product-p-22.html > https://example.com/en/product-p-22.html

在内部变成了`https://example.com/product_info.php?product_id=22&language=en 这是我的实际配置:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr)/
RewriteCond %{REQUEST_URI} .(php|html) [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ en/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)/$ index.php?language=$1&%{QUERY_STRING}
RewriteRule ^(.*)/(.*)-p-(.*).html$ product_info.php?products_id=$3&language=$1&%{QUERY_STRING}

我尝试了许多标志,例如 L, END, DPI 或这些标志的组合,但没有运气。 当我查看调试日志时,似乎找到了正确的 url,但随后重新开始解析默认 url:

pass through /var/www/example/product_info.php
init rewrite engine with requested uri /product-p-22.html
pass through /product-p-22.html

我在这里做错了什么?

Server version: Apache/2.4.41 (Ubuntu)

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite friendly-url


    【解决方案1】:

    在您的站点根目录 .htaccess 中使用这种方式:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} !\s/+(en|fr)/ [NC]
    RewriteRule ^ /en%{REQUEST_URI} [NC,R=301,L]
    
    RewriteRule ^([a-z]{2})/[^-/]+-p-([^./]+)\.html?$ product_info.php?language=$1&products_id=$2 [L,QSA]
    
    RewriteRule ^([a-z]{2})/$ index.php?language=$1 [QSA,L]
    

    【讨论】:

    • 我真的很喜欢它的简单性,不使用 rewritecond request_filename 会有什么好处/坏处吗?这里的顺序真的很重要吗?
    【解决方案2】:

    根据您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^en [NC]
    RewriteRule ^(.*)$ en/$1 [NC,R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php?language=en [NC,QSA,L]
    
    RewriteRule ^([^/]*)/([^-]*)-p-([^.]*)\.html$ product_info.php?products_id=$3&language=$1& [NC,L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 2016-06-06
      • 2012-02-22
      • 1970-01-01
      • 2011-07-24
      • 2016-05-18
      • 2023-03-18
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多