使用 mod_rewrite 重写动态 URL 时出现问题

发布于 2024-10-26 02:18:06 字数 534 浏览 2 评论 0原文

我正在尝试使用以下代码重写动态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_畞蕅 2024-11-02 02:18:06

我想我找到了解决方案。我正在使用以下内容:

RewriteRule ^([a-z0-9-]+)/?$ index.php?location=$1 [NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?location=$1&company=$2 [NC]

这似乎工作完美,并且成功重定向了 site.com/sandiego/ 和 site.com/sandiego/1/

I think I found the solution. I'm using the following:

RewriteRule ^([a-z0-9-]+)/?$ index.php?location=$1 [NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?location=$1&company=$2 [NC]

This seems to be working perfectly, and is successfully redirecting both site.com/sandiego/ and site.com/sandiego/1/

小情绪 2024-11-02 02:18:06

尝试这些:

RewriteRule ^([a-z0-9]+)/?$    index.php?location=$1 [NC]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?.*$    index.php?location=$1&company=$2 [NC]

请记住,在进行 URL 重写时尝试捕获任何字符序列(例如使用 (.*)(.+))通常是一个坏主意,正如该线程中所解释的:

Try those:

RewriteRule ^([a-z0-9]+)/?$    index.php?location=$1 [NC]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?.*$    index.php?location=$1&company=$2 [NC]

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

泪是无色的血 2024-11-02 02:18:06
Try  ([^\/]*)/([^\/]*)/$  instead of (.*)/(.*)/$
Try  ([^\/]*)/([^\/]*)/$  instead of (.*)/(.*)/$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文