URL重写包括结尾的斜线如果没有present [英] URL Rewrite Including Trailing Slash If Not Present

查看:104
本文介绍了URL重写包括结尾的斜线如果没有present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这有重写规则工作

RewriteBase /my/path/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my/path/index.php [L]

因此​​,与斜线工作的网址。 的http://本地主机/我/路/富/酒吧/
问题是,没有结尾的斜线的网址将打破相对链接。另外忽略了最低好看。

So URLs with a trailing slash work. http://localhost/my/path/foo/bar/
The problem is that URLs without the trailing slash will break relative links. Plus it dosen't look good.

这达到内部重定向的最大数量。

This reaches the maximum number of internal redirects.

RewriteRule ^/my/path/(.*[^/])$ $1/ [R]
RewriteRule . /my/path/index.php [L]

和这将做... 的http://localhost/my/path/index.php/bar/ <​​/ code>

And this will do... http://localhost/my/path/index.php/bar/

RewriteRule . /my/path/index.php
RewriteRule ^/my/path/(.*[^/])$ $1/ [R,L]

任何意见或解决方案?

Any Ideas or solutions?

推荐答案

的mod_rewrite 的混乱的特点是,内部重定向后的连一个合格的有 [L] 的整套规则是从一开始就重新处理

The confusing feature of mod_rewrite is that, after an internal redirect, even one qualified with [L], the entire set of rules is processed again from the beginning.

因此​​,您重定向一个不存在的路径的index.php ,但随后在添加斜线球的规则,你没有得到你想要的结果。

So you redirect a nonexistent path to index.php, but then the rules for adding a slash kick in and you don't get the result you want.

在你的情况,你只需要简单地把文件不存在条件对双方的重定向规则:

In your case you simply need to put the file nonexistence condition on both of the redirect rules:

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /my/path/index.php [L]

或许推动这一状况到文件的顶部:

Or maybe move this condition to the top of the file:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]   # redirect to same location to stop processing

RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

RewriteRule ^ /my/path/index.php [L]

还有一个未公开招内部重定向,应让更多的复杂的规则集更容易编写后停止加工 - 使用 REDIRECT_STATUS 环境变量,这是后一组内部重定向:

There's also an undocumented trick to stop processing after an internal redirect which should make more complex rulesets easier to write – using the REDIRECT_STATUS environment variable, which is set after an internal redirect:

RewriteCond %{ENV:REDIRECT_STATUS} .  # <-- that's a dot there
RewriteRule ^ - [L]   # redirect to same location to stop processing

RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

RewriteRule ^ /my/path/index.php [L]

这篇关于URL重写包括结尾的斜线如果没有present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆