【问题标题】:Why does this mod_rewrite directive not work?为什么这个 mod_rewrite 指令不起作用?
【发布时间】:2013-09-07 03:17:16
【问题描述】:

由于服务器迁移,我正在尝试使用以下 mod_rewrite 代码将所有以 /rc/roundcube/sm/squirrelmail 开头的 URI 定向到 https://webmail.{HTTP_HOST}

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{REQUEST_URI} ^/(rc|roundcube|sm|squirrelmail)(\/)?(.*)?$
RewriteRule ^/(.*) https://webmail.%1 [R=302, L]

因为我希望这个重定向影响机器上的所有虚拟主机,所以我将它放在 Apache 的 httpd.conf 文件中的虚拟主机指令中。

服务器确实启用了 mod_rewrite,但上面的这个似乎什么也没做!

也许有人能发现我的错误?

预期结果的一个例子是:

http://www.mydomain.com/rc --> https://webmail.mydomain.com

它应该能够处理带有或不带有www 或尾随/ 的任务,并且可能在用户为原始网址添加书签时包含参数。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    我认为,如果您只是将其放在 httpd.conf 文件中,则该规则没有上下文。尝试将其包装在某个位置或目录中(位置可能更好):

    <Location "/">
      RewriteEngine on
      RewriteCond %{REQUEST_URI} ^/(rc|roundcube|sm|squirrelmail)(/)?(.*)?$
      RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
      RewriteRule ^/(.*) https://webmail.%2 [R=302,L]
    </Location>
    

    并去掉R=302,之后的空格

    【讨论】:

    • 好节目乔恩,它是展示停止的空间,虽然还没有,我现在收到Firefox can't find the server at webmail.rc.
    • @PeterSnow 啊!条件需要有不同的顺序,反向引用需要是%2
    • 你是一位绅士和学者。谢谢乔恩。现在完美了。
    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多