Mod重写尾部斜杠问题 [英] Mod-rewrite Trailing Slash Issue

查看:88
本文介绍了Mod重写尾部斜杠问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于该主题的信息似乎并不多,所以我将概述我的特定问题,然后也许我们可以将问题和答案简化为更通用的内容.

There doesn't seem to be much info on this topic so I'm going to outline my specific problem then maybe we can shape the question and the answer into something a bit more universal.

我有这个重写规则

RewriteEngine On
RewriteBase /bookkeepers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?franchise=$1

哪个将更改此URL

http://example.com/location/kings-lynn

进入这个

http://example.com/location/index.php?franchise=kings-lynn

我遇到的问题是,如果我添加斜杠

The problem I am having is that if I add a trailing slash

http://example.com/location/kings-lynn/

然后将查询字符串返回为

then the query string is returned as

franchise=kings-lynn/

,由于某种原因,我的CSS和Javascript文件均未加载.

and for some reason none of my CSS and Javascript files are being loaded.

有什么想法吗?

推荐答案

正如@Paul Tomblin所说,.+很贪婪;也就是说,它尽可能地匹配.

As @Paul Tomblin said, the .+ is being greedy; that is, it's matching as much as it can.

^(.+[^/])/?$告诉它匹配任何内容,后跟一个不是/的字符,然后是一个可选的/.这具有不捕获尾随/的效果.

^(.+[^/])/?$ tells it to match anything, followed by a character that isn't a /, then followed by an optional /. This has the effect of not capturing the trailing /.

CSS和Javascript不起作用的最可能原因是您使用的是相对路径,例如src ="my.js".当有斜杠时,它看起来像一个目录,因此您的浏览器将查找/location/kings-lynn/my.js.您只需使用文件的绝对路径(例如/location/my.js)即可解决此问题.

The most probable reason your CSS and Javascript doesn't work is you're using a relative path, like src="my.js". When there's a trailing slash, it looks like a directory, so your browser will look for /location/kings-lynn/my.js. You can fix this simply by using an absolute path to your files (e.g. /location/my.js).

这篇关于Mod重写尾部斜杠问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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