.htaccess 重写,适用于所有目录
因此,我有由 url 传递的错误,例如
index.php?error=nojs
将由 PHP 解析以返回错误消息,例如: 请启用 Javascript
我在 .htaccess 中使用以下行以使 url 更易于
RewriteRule ^ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
管理让我的网址看起来像这样: site.com/ERROR_nojs
问题是,这只适用于根目录,
index.php?error=nojs works fine however
test/index.php?error=nojs does not?
那么如何转换每个目录的变量呢?
谢谢。 (我的原始脚本处理数百个错误并过滤掉可能对输出给用户有用的错误。将它们重定向到索引只是为了让他们可以看到其中包含错误消息的小错误是愚蠢的)
EDIT:
正如 Shai Mishali 指出的在 ERROR 之前删除“^”就可以了。
RewriteRule ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
但我忘了告诉你我还有另一个变量 ?page=
我需要获取该 vairbale 并将其添加到 url 中才能使其正常工作..
例如:
index.php?page=home&error=nojs
= site.com/home/ERROR_nojs
so
www.site.com/?page=home&error=nojs = www.site.com/home/ERROR_nojs
或
www.site/?page=about&error=unknown = www.site.com/about/ERROR_unknown
So I have errors which are passed by the url, for example
index.php?error=nojs
will then be parsed by PHP to return an error message, for example: Please enable Javascript
I'm using the following line in my .htaccess to make the url easier to manage
RewriteRule ^ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
It makes my URL look like this:
site.com/ERROR_nojs
The problem is, this only works for the root,
index.php?error=nojs works fine however
test/index.php?error=nojs does not?
So how can I convert the variable for every directory?
Thank you. (My original script handles hundreds of errors and filters out ones that might be useful to output to the user. It would be stupid to redirect them to the index just so they can see a small with an error message in it)
EDIT:
as Shai Mishali pointed out removing the '^' before ERROR did the trick.
RewriteRule ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
But I forgot to tell you I have another variable ?page=
I need get that vairbale and add it to the url in order for this to work..
e.g:
index.php?page=home&error=nojs
= site.com/home/ERROR_nojs
so
www.site.com/?page=home&error=nojs = www.site.com/home/ERROR_nojs
or
www.site/?page=about&error=unknown = www.site.com/about/ERROR_unknown
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定您的问题是您的规则正在寻找以 ERROR ( ^ 符号)开头的内容。
/ERROR 以 error 开头,这在你的 root 中有效,但是
/tests/ERROR 以测试开头,因此它无法识别它。
尝试删除 ^ 符号,看看会发生什么。
谢伊。
I'm pretty sure your problem is your rule is looking for something that starts with ERROR (the ^ sign) .
/ERROR starts with error, which works in your root , but
/tests/ERROR starts with tests , so it won't recognize it.
Try removing the ^ sign and see what happens.
Shai.
您可以使用下面的代码
您的URL site.com/home/ERROR_nojs将被视为index.php?page=home&error=nojs,您可以通过GET方法获取值。了解更多信息
You can use below code
Your URL site.com/home/ERROR_nojs will treated as index.php?page=home&error=nojs and you can get the values by GET method. For more info