7

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!

0

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.

3
  • Can you explain what each line is doing (and why I don't need to rewrite the folder to the subdomain). Thanks.
    – Cameron
    Mar 24 '15 at 12:56
  • I have updated the answer. Try the lines and see of they work. Also, do you have mod_rewrite active on your server? Have you checked your error logs?
    – Ciprian
    Mar 24 '15 at 13:05
  • Tried that but doesn't do anything. Forums are sill accessed at /forums and the sub domain just acts like a wildcard and doesn't rewrite.
    – Cameron
    Mar 25 '15 at 13:54
0

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

1
  • I really don't want to use MU for this... so if it's possible I'd like to do it with the htaccess file... Thanks.
    – Cameron
    Mar 27 '15 at 9:47
0

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>
3
  • Doesn't work. http://forums.example.com/ and http://www.example.com/ show the home page. And http://forums.example.com/forums/ and http://www.example.com/forums/ show the forums.
    – Cameron
    Apr 1 '15 at 20:29
  • What is Apache version? Apr 2 '15 at 3:58
  • @ tutankhamun Apache: 2.2.22
    – Cameron
    Apr 2 '15 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.