如何为这些子域链接设置URL重写?

时间:2012-06-27 05:38:27

标签: php http .htaccess url-rewriting

我有一个网站(比如名字www.manoj.com),我在子域名上创建了一个论坛

forum.manoj.com

现在,我想将manoj.com/forum/{anything else}之后的所有链接重定向到forum.manoj.com/{anything else}

我该怎么做。

由于

1 个答案:

答案 0 :(得分:3)

如果您想将example.com/forum重定向到forum.example.com,请在[document_root]/forum/.htaccess中写一下:

RewriteEngine On
RewriteBase /forum
RewriteCond %{REMOTE_HOST}  !=forum.example.com
RewriteRule (.*) http://forum.example.com/$1 [R=301,NE,L]

R=301标志表示它是“永久”重定向,Apache将发送标题HTTP/1.1 301 Moved Permanently

如果你真的无法在那里添加.htaccess文件,请在[document_root]/.htaccess写一下:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST}  !=forum.example.com
RewriteRule forum/(.*) http://forum.example.com/$1 [R=301,NE,L]

但这还不够。您需要为forum.example.com设置DNS记录,并在服务器上设置虚拟主机(如果您使用Apache),然后才能运行。通常,虚拟主机在httpd.conf中定义。有关详情,请参阅this