【问题标题】:Redirect to trailng slash (htaccess)重定向到斜杠 (htaccess)
【发布时间】:2011-09-01 08:26:21
【问题描述】:

尝试在每个链接中添加斜杠。即 http://mysite.com/products 应该使 301 重定向到 http://mysite.com/products/ 等。但是如何?这是htaccess:

RewriteEngine on
DirectoryIndex index.php
Options -Indexes
RewriteBase /

RewriteCond %{REQUEST_URI} \.css$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/css_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_URI} \.js$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/js_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
RewriteRule /admin/(.*)$ /admin/index.php

需要帮助!

【问题讨论】:

    标签: .htaccess apache2


    【解决方案1】:

    这是我正在使用的

    RewriteEngine on
    RewriteBase /
    
    ### CHECK FOR TRAILING SLASH - Will ignore files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
    

    基本上,这可以确保它不会向文件添加尾随,而只会添加文件夹或路径。

    编辑

    使其独立于域

    RewriteRule ^(.*)$ $1/ [L,R=301]
    

    【讨论】:

    • 将最后一行 RewriteRule ^(.*)$ example.com/$1 [L,R=301] 替换为 RewriteRule ^(.*)$ $1/ [L,R=301]
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多