【问题标题】:Add trailing slash to url using .htaccess使用 .htaccess 将斜杠添加到 url
【发布时间】:2014-10-19 13:40:48
【问题描述】:

我想为网址的深层添加斜杠

假设我有这个网址

mysite.com/work

当用户访问该网址时,我想被重定向到:

mysite.com/work/(我想这样做)

但我只想要深层,而不是 .html 页面

mysite.com/testing.html/(我不希望发生这种情况)

我有这个 .htaccess 规则,但这也会在我的 .html 页面中添加斜杠。我不想要那个。

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

有什么帮助吗?

【问题讨论】:

    标签: apache .htaccess trailing-slash


    【解决方案1】:

    您可以使用此规则为非文件添加斜杠:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
    

    【讨论】:

    • 它仍在向我的 html 文件添加斜杠。例如 test.html/ 我只想要路径或文件夹
    猜你喜欢
    • 2012-03-31
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    相关资源
    最近更新 更多