htaccess - 强制尾随斜杠两个目录深

发布于 2024-09-14 20:11:34 字数 839 浏览 6 评论 0原文

我的网站结构有一个根 /index.php,一些文件为 /directory/index.php ,一些文件为 /directory/(filename).php

我有以下 .htaccess ,它删除了 php 扩展名和“index.php”我的网址,并强制在第一级目录上添加斜杠以实现 SEO 效果:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

因此以下内容有效(它们显示正确的页面):

/
/directory/
/directory/filename/

唯一不起作用的是,如果我输入:

/directory/filename

它会转到:

http://(mylocalurl)/Users/(myusername)/Sites/(mysitedirectory)/directory/filename/

我的问题是:如何进行第二级文件名重写以强制尾随斜杠,例如:

/directory/filename/

感谢您的帮助!

My website structure has a root /index.php, some files as /directory/index.php and some as /directory/(filename).php

I have the following .htaccess which removes the php extensions and the "index.php" for my URLs, and forces trailing slashes on the first level directories for SEO goodness:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

so the following are working (they show the correct page):

/
/directory/
/directory/filename/

The only thing that doesn't work, is if I type in:

/directory/filename

It goes to:

http://(mylocalurl)/Users/(myusername)/Sites/(mysitedirectory)/directory/filename/

My question is: How do I make the second level filename rewrite to force a trailing slash like:

/directory/filename/

Thanks for your help!

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

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

发布评论

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

评论(3

十秒萌定你 2024-09-21 20:11:34
DirectorySlash on

将在适当的地方添加尾部斜杠。

DirectorySlash on

will add a trailing slash where appropriate.

沙与沫 2024-09-21 20:11:34

不是 100% 确定,但它可能是由您的第二个 RewriteCond 触发的,RewriteRule 运行,不替换任何内容,然后您有 [L] 使其不会转到最后一个 RewriteCond。

也许改变两者的顺序可能会有所帮助?

(否则,您可能需要研究 Multiviews 指令)

Not 100% sure, but it could be that it is triggered by your the second RewriteCond, the RewriteRule is run, not replacing anything, and then you have the [L] that makes it not go to the last RewriteCond.

Maybe changing the order of the two might help?

(Or else, you might want to look into Multiviews directive)

枯叶蝶 2024-09-21 20:11:34

当您指定要执行外部重定向的本地相对 URL 路径时,需要在该路径前面添加斜杠。由于与您的 RewriteRule 测试模式的匹配不会有前导斜杠,因此请务必在重写中添加一个斜杠:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ /$0/ [L,R=301]

如果我正确理解了您的问题,那么这是您遇到的唯一问题。如果您需要其他东西,请告诉我。

When you're specifying a local relative URL path to perform an external redirection on, that path needs to be prepended with a slash. Since the match to your RewriteRule test pattern will not have a leading slash, be sure to put one in the rewrite:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ /$0/ [L,R=301]

If I've understood your problem correctly, that's the only issue you have. If you needed something else though, let me know.

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