【问题标题】:.htaccess redirect stripping slash.htaccess 重定向剥离斜线
【发布时间】:2015-11-08 09:36:49
【问题描述】:

我就是想不通。我的 .htaccess 规则如下所示:

RewriteEngine On

RewriteBase /

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,QSA,L]

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

RewriteRule ^(en|de)(/.*)?$ $2?lang=$1 [L,NC]

我的计划显然是重定向如下:

所以最后一条规则按预期工作。奇怪的行为是对http://www.example.com/testhttp://example.com/test 的请求被重定向到https://example.comtest。看起来规则 RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1%{SERVER_NAME} 和请求 uri $1 之间的斜线只是没有添加。

如果有人能指出正确的方向,我会很高兴。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以在根 .htaccess 中使用这些规则:

    RewriteEngine On
    
    ## remove www and turn on https in same rule
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
    
    RewriteRule ^(en|de)(/.*)?$ $2?lang=$1 [L,NC,QSA]
    

    请务必在清除浏览器缓存后对其进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-27
      • 2016-06-06
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2016-10-29
      相关资源
      最近更新 更多