【问题标题】:how to redirect FIRST and then rewrite using mod_rewrite如何重定向 FIRST 然后使用 mod_rewrite 重写
【发布时间】:2011-03-18 06:22:17
【问题描述】:

我有两个域指向同一个主机,例如 example1.com 和 example2.com。

我已经有一个重定向:

重写规则 ^([A-Za-z0-9-]+)/$ page.php?q=$1 [L]

捕获诸如 example1.com/hello-world/ => example.com/page.php?q=hello-world 之类的东西

现在我需要满足 3 个具体条件:

1) example1.com/special/ => example2.com/special/ [仅适用于“特殊”]
2) example2.com/ => example2.com/special/
3) example2.com/anything-not-special/ => example1.com/anything-not-special/

我可以做到这一点,除了我希望在 URL 栏中准确显示右侧列中的内容。相反,我得到: example2.com/special/ => example1.com/page.php?q=special

这对我来说并不理想。

任何帮助表示赞赏,谢谢!

【问题讨论】:

    标签: apache mod-rewrite url-rewriting redirect


    【解决方案1】:

    看看这是不是你想要的:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/page\.php
    RewriteCond %{HTTP_HOST}%{REQUEST_URI} =example1.com/special/ [OR]
    RewriteCond %{HTTP_HOST}%{REQUEST_URI} =example2.com/
    RewriteRule ^.*$ http://example2.com/special/ [R=301,L]
    
    RewriteCond %{REQUEST_URI} !^/page\.php
    RewriteCond %{HTTP_HOST}    =example2.com
    RewriteCond %{REQUEST_URI} !=/special/
    RewriteRule ^.*$ http://example1.com%{REQUEST_URI} [R=301,L]
    
    RewriteRule ^([A-Za-z0-9-]+)/$ page.php?q=$1
    

    【讨论】:

    • 感谢您的回答,但是没有用。似乎第二个块中的第二个 RewriteCond 没有正确捕获 /special/。此外,它仍然将 URL 栏中的 URL 解析为 example1.com/page.php?q=special
    • 是的,对不起。我的小疏忽。看看修改后的版本是否效果更好。
    • example2.com/special/ 仍在解析为 example1.com/page.php?q=special 这很奇怪。这对我来说没有意义 - 你的代码是合理的,几乎和我已经摸索到的一样。
    • 所以当您转到example2.com/special/ 时,它仍然会将浏览器中的URL 更改为example1.com/page.php?q=special?嗯..它在我的测试服务器上没有这样做。您知道的任何其他模块是否还有其他规则?我试图思考还有什么可能会迫使外部重定向..
    • 嗯,很有趣。我搞砸了一点,无法弄清楚为什么会发生这种情况……可能是我只是忽略了一些事情。至少你现在能够让它工作;如果我弄清楚问题的根源可能是什么,我会告诉你的。
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2011-04-14
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多