mod_rewrite 被忽略
我正在尝试将“domain.com/index.php?site=food&category=beef”转换为“domain.com/food/beef”,但无论我如何尝试,它都不起作用。它总是离开原始域,并且我没有收到任何错误。
我认为这是我的错,我在 3 个不同的服务器(和 3 个不同的项目)上尝试了 3 个不同的 URL...似乎我不明白 mod_rewrite 的真正工作原理,尽管我阅读了我发现的有关该主题的所有文档。我什至在这里花了几天时间,但没有找到任何解决方案。
服务器上启用了 Mod_rewrite:
RewriteEngine On
RewriteRule ^ http://www.google.com [R,L]
给我“http://www.google.com/?site=food&category=beef”。看起来 mod_rewrite 无法识别查询字符串...所以我尝试了使用 RewriteCond %{QUERY_STRING} 的几种解决方案...但没有任何效果:/
希望你们能帮助我!我快要疯了!
提前致谢!
I'm trying to transform "domain.com/index.php?site=food&category=beef" into "domain.com/food/beef" but it does not work, no matter what I try. It always leaves the original domain and I get no errors.
I think it's my fault, I tried this for 3 different URLs on 3 different servers (and 3 different projects)... it just seems like I don't get how mod_rewrite really works, though I read every documentation on this topic I found. I even spent days here on SO without finding any solution.
Mod_rewrite is enabled on the server:
RewriteEngine On
RewriteRule ^ http://www.google.com [R,L]
gives me "http://www.google.com/?site=food&category=beef". It looks like mod_rewrite does not recognise the query string... So I tried several solutions with RewriteCond %{QUERY_STRING}... but nothing works :/
Hopefully you guys can help me! I'm going insane on this!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
或者更一般地说:
Try:
Or more generally:
你想做这样的事情吗?
这将使当您访问 http://domain.com/food/beef 时,请求会得到内部重写为“/index.php?site=food&category=beef”,index.php 用于服务原始请求。浏览器的地址栏仍会显示“http://domain.com/food/beef”。
如果您希望地址栏显示 http://domain.com/index.php ?site=food&category=beef 然后在“[L]”中添加“R,”。如果这是向后的,并且您想要这样,当有人输入 http://domain.com 时/index.php?site=food&category=beef 在地址栏中,并且请求在服务器内部被重写为“/food/beef”,那么您需要使用 RewriteCond 解析出查询字符串:
同样的事情也适用于像第一个示例一样,“R”导致浏览器重定向。如果您希望地址栏更改为 http://domain.com/food/beef 那么括号应如下所示:
[L,R]
。请注意,您需要在规则中的目标末尾添加 ?,这样查询字符串就不会被抛出。这就是为什么在您的 google 示例中附加了查询字符串。编辑:
看到您只是想更改浏览器位置栏中的内容而不是内容所在的位置:
您需要重新重写上面的第二条规则重写了BACK到index.php,但没有重定向。为了防止两条规则无限循环,因为一条规则会重写另一条规则,反之亦然,您需要在某处添加一个标志,以防止上面的第二条规则一遍又一遍地重定向您。
因此,将两者结合起来,您将得到以下结果:
请注意查询字符串中的
redirected
参数。当有人尝试访问 URL 的干净版本(例如“/food/beef”)时,就会插入此内容。在内部,它被重新路由到index.php,但由于该规则没有“R”,因此浏览器的位置栏不会改变。第二条规则现在检查请求的查询字符串中是否包含
redirected
参数。如果没有,则意味着有人在浏览器的位置栏中输入了index.php url,因此将浏览器重定向到干净版本。Are you trying to do something like this?
This will make it so when you go to http://domain.com/food/beef the request gets rewritten to "/index.php?site=food&category=beef" internally and index.php is used to serve the original request. The browser's location bar will still say "http://domain.com/food/beef".
If you want the location bar to say http://domain.com/index.php?site=food&category=beef then add an "R," to the "[L]". If this is backwards and you want it so when someone enters http://domain.com/index.php?site=food&category=beef in the location bar, and the request gets rewritten to "/food/beef" internally on the server, then you need to parse out the query string using RewriteCond:
The same thing applies with the "R" causing a browser redirect like the first example. If you want the location bar to change to http://domain.com/food/beef then the brackets should look like:
[L,R]
. Note that you need a ? at the end of the target in the rule, so that query strings don't get thrown in. That is why in your google example, the query string is being appended.EDIT:
Seeing as you just wanted to change what's in the browser's location bar and not where the content is:
You need to re-rewrite what the 2nd rule above has rewritten BACK to index.php, but without a redirect. In order to keep the 2 rules from looping indefinitely because one rule rewrites to the other rule and vice versa, you need to add a flag somewhere to keep the 2nd rule above from redirecting you over and over again.
So combining the two, you'll have this:
Note the
redirected
parameter in the query string. This gets inserted when someone tries to access the clean version of the url, e.g. "/food/beef". internally, it gets rerouted to index.php but since the rule doesn't have a "R", the browser's location bar doesn't change.The second rule now checks if the request contains the
redirected
param in the query string. If it doesn't, that means someone entered in their browser's location bar the index.php url, so redirect the browser to the clean version.