使用 mod_rewrite 重写动态 URL 时出现问题
我正在尝试使用以下代码重写动态 URL:
RewriteRule (.*)/$ index.php?location=$1
RewriteRule (.*)/(.*)/$ index.php?location=$1&company=$2
正如您从上面的代码中看到的那样,我确实需要两次重写。首先,我需要页面重写位置(如果仅此而已)。例如,它将把 site.ccom/index.php?location=sandiego 转换为 site.com/sandiego/
这是正常工作的。
但是,当我尝试添加第二个变量时,它失败了。它设法检测到 &company=1 变量,但由于某种原因,它返回 ?location=sandiego 变量作为“index.php”。例如,如果我输入以下内容:site.com/san-diego/1/,然后尝试抓取$location和$company变量,它只会成功获取$company变量,并且$location变量将被设置到“index.php”,而不是“圣地亚哥”。
任何帮助将不胜感激。
I'm trying to rewrite a dynamic URL using the following code:
RewriteRule (.*)/$ index.php?location=$1
RewriteRule (.*)/(.*)/$ index.php?location=$1&company=$2
I really need two rewrites as you can see from the above code. First, I need the page to rewrite just the location if that's all there is. For example, it would turn site.ccom/index.php?location=sandiego into site.com/sandiego/
This is working correctly.
However, when I try to add in the second variable it fails. It manages to detect the &company=1 variable, but for some reason it returns the ?location=sandiego variable as 'index.php'. For example, if I input the following: site.com/san-diego/1/ and then try to grab the $location and $company variables, it will only get the $company variable successfully, and the $location variable will be set to 'index.php', rather than 'san-diego'.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我找到了解决方案。我正在使用以下内容:
这似乎工作完美,并且成功重定向了 site.com/sandiego/ 和 site.com/sandiego/1/
I think I found the solution. I'm using the following:
This seems to be working perfectly, and is successfully redirecting both site.com/sandiego/ and site.com/sandiego/1/
尝试这些:
请记住,在进行 URL 重写时尝试捕获任何字符序列(例如使用
(.*)
或(.+)
)通常是一个坏主意,正如该线程中所解释的:Try those:
Keep in mind that trying to capture any sequence of characters (by using for example
(.*)
or(.+)
) when doing URL rewriting, is usually a bad idea, as explained in this thread : http://www.sitepoint.com/forums/apache-configuration-199/rewrite-eliminate-trailing-slash-arbitrary-parameters-520302.html#post3657601