[英]mod_rewrite and trailing slashes
I have 我有
RewriteRule ^([A-Za-z0-9-_≈]+)/?$ page.php?text=$1
to translate hits like domain.com/sample_text
into page.php?text=sample_text
可以将
domain.com/sample_text
匹配项转换为page.php?text=sample_text
However, I want the following two conditions to also work (and give the same result). 但是,我希望以下两个条件也能起作用(并给出相同的结果)。
1) If they go to domain.com/sample_text/
I want to keep the trailing slash in the adress bar and show page.php?text=sample_text
(now it goes to the right page but the css and js-paths are messed up). 1)如果他们转到
domain.com/sample_text/
我想在地址栏中保留斜杠并显示page.php?text=sample_text
(现在转到正确的页面,但是CSS和JS路径被弄乱了)。
2) If they go to domain.com/sample_text/worthlessdata123
I want to strip everything after the /, keeping domain.com/sample_text/
in the adress bar and show page.php?text=sample_text
. 2)如果他们转到
domain.com/sample_text/worthlessdata123
我想删除/之后的所有内容,并将domain.com/sample_text/
保留在地址栏中,并显示page.php?text=sample_text
。
I've struggled quite a bit, but I'm lousy at regex and my head isn't working properly. 我已经挣扎了很多,但是我对正则表达式很不满意,而且我的头工作不正常。
The rules you have should suffice for the first condition. 您拥有的规则应足以满足第一个条件。 If someone goes to
domain.com/sample_text/
, the URI sample_text/
matches and should get served the content at /page.php?text=sample_text
while the browser's URL address bar remains unchanged. 如果有人访问
domain.com/sample_text/
,则URI sample_text/
匹配,并且应该在/page.php?text=sample_text
处获取内容,而浏览器的URL地址栏保持不变。
For the second condition, you need to add a rule to handle that: 对于第二种情况,您需要添加一条规则来处理:
RewriteRule ^([A-Za-z0-9-_≈]+)/.+$ /$1/ [L,R=301]
This will redirect: domain.com/sample_text/worthlessdata123
to domain.com/sample_text/
, then the first rule should take things from there. 这将重定向:
domain.com/sample_text/worthlessdata123
到domain.com/sample_text/
,然后第一个规则应该从那里开始。
Misread what your first issue was. 误读了您的第一个问题。 The relative/absolute paths you have in your page content is getting a different base because of the extra slash.
由于额外的斜杠,页面内容中的相对/绝对路径的基准有所不同。 When you have something like
<img src="images/blah.gif">
, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). 当您具有类似
<img src="images/blah.gif">
,相对基数是从浏览器看到的URL派生的(不是服务器内部重写的内容)。 So if the browser sees: http://domain.com/sample_text
, the URI base is /
, but when the browser sees http://domain.com/sample_text/
, the URI base becomes /sample_text/
, which I'm guessing isn't where somethin glike images/blah.gif
resides. 因此,如果浏览器看到:
http://domain.com/sample_text
: http://domain.com/sample_text
,则URI基为/
,但是当浏览器看到http://domain.com/sample_text/
,URI基为/sample_text/
,即猜测并不是glike images/blah.gif
所在的地方。
You can fix this by including the base in the header of your pages: 您可以通过在页面标题中添加基本内容来解决此问题:
<base href="/">
The first problem you need to solve by using absolute paths for your assets (js, images, etc.) 您需要通过为资产(JS,图像等)使用绝对路径来解决的第一个问题
For the second problem, you need @Jon Lin's solution. 对于第二个问题,您需要@Jon Lin的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.