RewriteRule,url显示错误
我是 mod_rewrite 新手,遇到这个问题:
我有一个使用 mod_rewrite 的工作重定向,我的 .htaccess:
RewriteEngine on
RewriteRule ^microsite/([^/\.]+)/$ micrositecontroller.php?name=$1 [L]
回显文本
micrositecontroller.php 仅在输入 URL 时在浏览器上
:本地主机/项目/微型站点/测试/
我被重定向到想要的地方,但是当我输入时:
本地主机/项目/微型站点/测试
它仍然重定向到想要的地方,但 URL 变成这样:
localhost/project/microsite/test/?name=test
现在我想要的是尾随的“/?name=test”不显示。
我尝试了正则表达式的不同组合,但无济于事,我不知道这是否正常。有什么想法吗?
I am new to mod_rewrite and I have this problem:
I have a working redirect with mod_rewrite, my .htaccess:
RewriteEngine on
RewriteRule ^microsite/([^/\.]+)/$ micrositecontroller.php?name=$1 [L]
micrositecontroller.php only echoes a text
On the browser when I enter my URL:
localhost/project/microsite/test/
I am redirected to where wanted but when I enter:
localhost/project/microsite/test
It still redirects to where wanted but the URL becomes like this:
localhost/project/microsite/test/?name=test
Now what I want is that trailing "/?name=test" not to show up.
I tried different combinations of the regex but to no avail and I have no idea if it is normal of not. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的正则表达式中有一个斜杠,所以你的正则表达式处理斜杠......只需将其删除:
You have a slash in your regex, so your regex handles slash... just remove it:
您想要的是:
容纳以斜线和不斜线结尾的两种情况。打开(或关闭)斜杠的问题是 apache 强制浏览器重定向,从而使
?name=test
显示在浏览器的地址栏中。这是因为 mod_dir 和
DirectorySlash on
指令相互干扰。 DirectorySlash 指令告诉 apache 在访问看起来像是目录的内容时将浏览器重定向(在您的情况下,将localhost/project/microsite/test
重定向到 smae URI,除了尾随斜杠之外。What you want is:
To accommodate both ending with a slash and no slash. The problem with leaving the slash on (or off) is apache forces a browser redirect, thus making the
?name=test
show up in the browser's location bar.This is because mod_dir and the
DirectorySlash on
directive is interferring. The DirectorySlash directive tells apache to redirect the browser when it accesses what looks to be a directory (in your caselocalhost/project/microsite/test
to the smae URI except with a trailing slash.