2

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.

Community
  • 1
  • 1
James
  • 15,754
  • 12
  • 73
  • 91

2 Answers2

1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This doesn't work for me. It is the only rule in my `.htaccess` file, which is in `/var/www/forum/`. I've done a hard-refresh but if I go to example.net/forum/viewtopic.php?f=2&t=436 it doesn't redirect me. – James Sep 20 '15 at 06:29
0
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "^www.example.net$" [OR]
RewriteCond "%{HTTP_HOST}" "^example.net$"
RewriteRule ^/?forum/(.*)$ http://forum.example.net/$1 [R=301]
BareNakedCoder
  • 3,257
  • 2
  • 13
  • 16
  • this did not work for me. I.e nothing has changed since I added it. I am correct in assuming that the .htaccess file I add this to is the one in the forum directory? – James Sep 20 '15 at 02:55