James
James

Reputation: 16359

Using .htaccess to redirect URL to subdomain

I'm hosting a forum and have recently decided to move it to a sub-domain instead.

As such, I moved all the forum files in to var/www/forum and I can access the forums just fine through this directory, i.e example.net/forum.

I've also set up the sub-domain in my apache virtual host so that if I go to forum.example.net it will display the forums to me and this also works fine.

All I need to do now is set up a .htaccess to redirect people using the directory URL example.net/forum to forum.example.net.

This answer gets me close, but not quite there as it will continually redirect me.

Ideally I'd like to carry their request over too, e.g example.net/forum/viewtopic.php?example gets redirected to forum.example.net/viewtopic.php?example.

Throughout my search for the answer, mod-rewrite may work but I am not too sure how to implement it.

Any help would be appreciated.

Upvotes: 2

Views: 2121

Answers (2)

anubhava
anubhava

Reputation: 786291

Inside /forum/.htaccess you can use this rule as your first rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.net)$ [NC]
RewriteRule ^(.*)$ http://forum.%1/$1 [R=301,L,NE]

Upvotes: 1

CodeBoy
CodeBoy

Reputation: 3300

RewriteEngine On
RewriteCond "%{HTTP_HOST}" "^www.example.net$" [OR]
RewriteCond "%{HTTP_HOST}" "^example.net$"
RewriteRule ^/?forum/(.*)$ http://forum.example.net/$1 [R=301]

Upvotes: 0

Related Questions