如何为此类子域链接设置 URL 重写


How to set a URL rewrite for sub-domain links like these?

我有一个网站(说www.manoj.com的名字),我创建了一个关于子域的论坛

forum.manoj.com

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

我该怎么做。

谢谢

如果要将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 中定义。欲了解更多信息,请参阅此处