Use subdomain for certain urls

I have the following rules in my .htaccess file on a WordPress (single installation):

# REWRITE FORUMS DOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^forums\.example\.com$
RewriteRule !^forums/? forums%{REQUEST_URI} [NC,L]

# REWRITE FORUMS FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/forums/([^\s]*) [NC]
RewriteRule ^ http://forums.example.com/%1 [R=301,L]

This is so that the forums that exist at http://example.com/forums are accessed at http://forums.example.com/

However the server just blows up with a 500 server error…

Any ideas on how to do this? These rules work perfectly when used on non-wordpress sites… The second rewrite rules successfully send /forums to the sub domain but the subdomain doesn’t seem to be able to pass the data into WordPress correctly.’

The whole htaccess file looks like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# REWRITE FORUMS DOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^forums\.example\.com$
RewriteRule !^forums/? forums%{REQUEST_URI} [NC,L]

# REWRITE FORUMS FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/forums/([^\s]*) [NC]
RewriteRule ^ http://forums.example.com/%1 [R=301,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

The reason that the WordPress rules are after the forums rules are because if I put them after then they are never hit!

3 s
3

Try this (untested):

# BEGIN Forums Rewrite
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com
RewriteRule ^(.*)$ forums/%1
# END Forums Rewrite

It should work with both your requirements. You don’t need the second one (rewriting the forums folder to subdomain) because rewriting the actual forum ID will override the basic subdomain rewriting forums.domain.com/forumid=16.

You should add these lines after those generated by WordPress.

Leave a Comment