htaccess - 强制尾随斜杠两个目录深
我的网站结构有一个根 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将在适当的地方添加尾部斜杠。
will add a trailing slash where appropriate.
不是 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)
当您指定要执行外部重定向的本地相对 URL 路径时,需要在该路径前面添加斜杠。由于与您的
RewriteRule
测试模式的匹配不会有前导斜杠,因此请务必在重写中添加一个斜杠:如果我正确理解了您的问题,那么这是您遇到的唯一问题。如果您需要其他东西,请告诉我。
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:If I've understood your problem correctly, that's the only issue you have. If you needed something else though, let me know.