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

2019-08-29 07:24发布

问题:

I have a site ( say by name www.manoj.com ) and I have created a forum on sub-domain

forum.manoj.com

Now, I want to redirect all the links following manoj.com/forum/{anything else} to forum.manoj.com/{anything else}

How can I do it.

Thanks

回答1:

If you want to redirect example.com/forum to forum.example.com, in [document_root]/forum/.htaccess write:

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

The R=301 flag is to indicate it is a "permanent" redirect, Apache will send the header HTTP/1.1 301 Moved Permanently.

If you really cannot add a .htaccess file there, in [document_root]/.htaccess write:

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

But this is not enough. You need to set the DNS record for forum.example.com and set up virtual host (if you use Apache) on the server before it will work. Typically virtual hosts are defined in httpd.conf. For more see this