mod_rewrite`重定向`怎么样?

时间:2012-01-14 22:47:45

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

我有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时,该网址未被重定向到thinkingmonkey.me。浏览器地址栏中的网址仍为thinkingmonkey.com

我做错了什么?

2 个答案:

答案 0 :(得分: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,可以将此行添加到VirtualHost的{​​{1}}部分:

thinkingmonkey.com

答案 1 :(得分:0)

试试这个:

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

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