【问题标题】:Add trailing slash to url only works with non-ww redirect将尾部斜杠添加到 url 仅适用于非 www 重定向
【发布时间】:2015-04-14 17:17:05
【问题描述】:

我正在尝试在 url 中添加一个斜杠。它几乎可以工作,但有一个我不明白的小问题。

当我调用该站点http://domain.com/test > 重写到 www.domain.com > 添加尾部斜杠。

当我直接调用该站点http://www.domain.com/test > 没有添加任何内容。 对我来说重要的是 https:// 必须重定向到 https:// 而不是 http://

这是我的配置:

############################################

启用重写

Options +FollowSymLinks
RewriteEngine on    

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !.html
RewriteCond %{REQUEST_URI} !.php
RewriteCond %{QUERY_STRING} !price

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

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

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    用这些简化的规则替换你的规则:

    Options +FollowSymLinks
    RewriteEngine on    
    
    RewriteCond %{HTTPS}s on(s)|
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.+?)/?$ http%1://www.domain.com/$1/ [R=302,L,NE]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . /index.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 2017-09-28
      • 1970-01-01
      • 2023-03-17
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多