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!

Topic apache url-rewriting htaccess Wordpress

Category Web


I offer following solution. It's required prepending this lines to index.php.

if (!empty($_SERVER['REDIRECT_URL'])) {
    $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL'];
}

I know this is a dirty hack, but WordPress rewrite mechanism based on $_SERVER['REQUEST_URI'] value.

These rules tested only on links like (with another domain) http://example.com/forums/ and http://forums.example.com/ Let me know if it not works on other links.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# WordPress native stop rule
RewriteRule ^index\.php$ - [L]

# prepend subdomain prefix
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} ^forums\.example\.coml$
RewriteRule !^forums/? forums%{REQUEST_URI} [NC,L]

# redirect to subdomain
RewriteCond %{HTTP_HOST} !^forums\.example\.com$
RewriteCond %{REQUEST_URI} ^/forums/(.*) [NC]
RewriteRule ^ http://forums.example.com/%1 [R=301,L]

# WordPress native rewrite with avoiding existing file- and dir- links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# avoid rewrite endless loop (to be on the safe side)
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
</IfModule>

This is probably not very trivial to do. You need to take into account that wordpress needs the "forums" directory in the url to successfully detect that the requested content is forum post and not regular posts or pages. Therefor you need to change the value of REQUEST_URI in the htaccess file or add your own URL parsing code.

the path of least resistance for this kind of configuration may be to have a mutisite install and activate the forums on their own site. It probably will be more straight forward to share data themes and plugins between sites the to handle everything related to url rewriting and parsing yourself


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.