【问题标题】:mod_rewrite `Redirect` how?mod_rewrite `Redirect` 怎么样?
【发布时间】:2012-02-10 13:36:33
【问题描述】:

我有 2 个网址说 thinkingmonkey.methinkingmonkey.com 都有 ip-address 127.0.0.1 (A.K.A localhost)。

我想将任何请求重定向到thinkingmonkey.comthinkingmonkey.me

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /mysite
    ServerName thinkingmonkey.me
    ServerAlias www.thinkingmonkey.me
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
    Options -Indexes +FollowSymLinks
    RewriteEngine On
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    ServerName thinkingmonkey.com

    Redirect thinkingmonkey.com  http://thinkingmonkey.me/

  #  Redirect / http://thinkingmonkey.me/ #have even tried this

    ServerAlias www.thinkingmonkey.com
    RewriteEngine on
</VirtualHost>

当我尝试访问 thinkingmonkey.com 时,url 不会重定向到 thinkingmonkey.me。浏览器地址栏中的url仍然是thinkingmonkey.com

我做错了什么?

【问题讨论】:

    标签: apache mod-rewrite url-rewriting apache2 httpd.conf


    【解决方案1】:

    mod_rewrite 被认为可以更好、更强大地处理这些重写。您可以使用以下代码:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(.*\.)?thinkingmonkey\.com$ [NC]
    RewriteRule ^ http://%1thinkingmonkey.me%{REQUEST_URI} [L,R=301]
    

    如果您真的-真的想使用 mod_aias,可以将此行放在 thinkingmonkey.comVirtualHost 部分:

    Redirect 301 / http://thinkingmonkey.me/
    

    【讨论】:

      【解决方案2】:

      试试这个:

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !.*thinkingmonkey\.me$
      RewriteRule ^/(.*)$ http://thinkingmonkey.me/$1 [R]
      

      文档:http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-22
        • 2019-08-18
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多