.htaccess 重写为强制末尾斜杠

发布于 2024-12-10 03:00:55 字数 366 浏览 0 评论 0原文

我的 htaccess 文件中有以下代码:

# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]

当我访问 www.mydomain.com/test 时,它似乎工作正常,它将其重定向到 /test/。问题是当我访问 www.mydomain.com/test/another 时,它不会将尾部斜杠放在另一个上。

有谁知道如何修改我的代码,以使无论 URL 有多长,尾部斜杠都可以工作?

谢谢!

I have the following code in my htaccess file:

# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]

That seems to work fine when I go to www.mydomain.com/test it redirects it to /test/. The problem is when I go to www.mydomain.com/test/another it doesn't put the trailing slash on another.

Does anyone know how to modify my code to make the trailing slash work no matter how long the URL is?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

救星 2024-12-17 03:00:55

一个稍微更稳健的答案,基于上面的答案

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

RewriteCond将检查以确保没有具有该名称的文件,如果没有,则执行 RewriteRule。比手动扩展列表更面向未来!

A slightly more robust answer, based on the answer above:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

The RewriteCond will check to make sure there's no files with that name, and if not, perform the RewriteRule. More future-proof than having a manual list of extensions!

猫九 2024-12-17 03:00:55
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

编辑:如果您想排除某些请求,例如 php 文件:

RewriteCond %{REQUEST_URI}  !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

Edit: in the case you want to exclude some requests like for php files:

RewriteCond %{REQUEST_URI}  !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
终止放荡 2024-12-17 03:00:55

虽然死亡解决方案有效,但当您忘记将某些文件类型添加到列表中时,可能会很烦人。您可以执行此操作,以在条件中使用 !-f 强制所有不直接指向文件的 URL 结尾斜杠。

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

While Death's solution works it can be annoying when you forget to add certain file types to the list. You can do this to force trailing slash for all URLs that do not point directly to a file using !-f in the condition.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
因为看清所以看轻 2024-12-17 03:00:55

接受的答案对我不起作用。确实如此,来自 SEOMoz

# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

注意 RewriteBase /< /code> 对于每条规则。至少,当我删除它时,它就停止工作了。

The accepted answer didn't work for me. This did, from SEOMoz:

# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

Note the RewriteBase / for each rule. At least, when I removed it, it stopped working.

鹿童谣 2024-12-17 03:00:55

这对我来说非常有效。 (来自用户 Ajax 的评论)
其他链接的问题是我的 CSS 在应用重定向规则后停止工作,但 CSS 也可以使用下面的重写规则正常工作

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

完整代码

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    #Force Trailing slash
    RewriteRule ^((.*)[^/])$ $1/ [L,R=301]
</IfModule> 

This is working perfectly for me. ( from comment of user Ajax )
The problem with other links was my CSS stopped working after applying the redirect rule but CSS is also working fine with the below rewrite rule

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

Complete code

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    #Force Trailing slash
    RewriteRule ^((.*)[^/])$ $1/ [L,R=301]
</IfModule> 
荒芜了季节 2024-12-17 03:00:55
RewriteRule ^(.*)[^/]$ $1/ [L,R=301]

这应该工作得很好。它只是检查以确保尾随字符不是斜杠并添加一个。

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

This should work pretty well. It just checks to make sure the trailing character isn't a slash and adds one.

感受沵的脚步 2024-12-17 03:00:55

这对我来说效果很好,并且不依赖于对实际文件的评估,因为有些人建议使用“-f”标志:

RewriteCond %{REQUEST_URI} !\.[a-z0-9]+$ [NC]
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

This works just fine for me and doesn't rely on evaluating to an actual file as some have suggested the '-f' flag:

RewriteCond %{REQUEST_URI} !\.[a-z0-9]+$ [NC]
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
☆獨立☆ 2024-12-17 03:00:55

由于规则末尾强制斜线,上面的示例对我不起作用,例如 $1$2/ 。

对我来说这个工作(我将它用于 WordPress 并重定向到 HTTPS)。您必须在 RewriteEngine 和 RewriteBase 行后面添加条件和规则行:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# I added these two lines for redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress`

Above examples didn't work for me thanks to forced slash on the end of rule, e.g. $1$2/ .

For me worked this (I used it for wordpress and redirecting to HTTPS). You have to add the condition and rule lines just behind RewriteEngine and RewriteBase lines:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# I added these two lines for redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress`
清风不识月 2024-12-17 03:00:55

要强制尾部斜杠,您可以使用:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

注意在 mod-rewrite 之前运行的 mod-dir 模块在看到对 的请求时会自动添加尾部斜杠>existant dir ,因此我们必须从规则中排除目录,否则在某些服务器上使用不带 RewriteCond %{REQUEST_FILENAME} !-d 的规则,您最终会得到/dir// 或者如果您关闭了directorySlash,它可能会导致问题。

上面的规则为所有请求添加尾部斜杠,包括带有扩展名的文件,如果您不希望文件带有尾部斜杠,可以通过在规则上方添加以下条件来排除它们

RewriteCond %{REQUEST_FILENAME} !-f

To force the trailing slash, you can use :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

Note mod-dir module that runs before the mod-rewrite automatically adds a trailing slash when it sees a request for an existant dir ,so we have to exclude directories from the rule otherwise using the rule without RewriteCond %{REQUEST_FILENAME} !-d on some servers, you will end up at /dir// or it can cause problems if you have turned off the directorySlash .

The rule above adds a trailing slash to all requests including files with extension, if you dont want your files with a trailing slash, you can exclude them by adding the following condition above the Rule

RewriteCond %{REQUEST_FILENAME} !-f
稀香 2024-12-17 03:00:55
<rule name="Remove trailing slash" stopProcessing="true">
    <match url="^([^.]+)/$" />

将此规则添加到您的配置文件中,它对我有用

<rule name="Remove trailing slash" stopProcessing="true">
    <match url="^([^.]+)/$" />

add this rule in your config file and it is working for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文