admin管理员组

文章数量:1122846

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

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

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

This is so that the forums that exist at are accessed at /

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\$
RewriteRule !^forums/? forums%{REQUEST_URI} [NC,L]

# REWRITE FORUMS FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/forums/([^\s]*) [NC]
RewriteRule ^ /%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!

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!

Share Improve this question edited Mar 25, 2015 at 13:52 Cameron asked Mar 24, 2015 at 12:40 CameronCameron 3395 silver badges15 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

If I can offer an alternative solution, there is a plugin in the repository that will map domains or subdomains on the WordPress side. I use the Multiple Domains plugin by Matthias Wagner to do something very similar on one of my sites.

To set this up, just have the subdomain point to your WordPress installation, which you apparently already do. Within the plugin settings, add your mapping to map http://forums.example.com to http://www.example.com/forums.

Using this plugin (or another like it) has the added benefit that WordPress “recognizes” the mapping such that your forum posts will report their canonical URI with the subdomain rather than the main domain. This may not be critical in your application, but it will help search engines and other bots to identify your forum posts with your chosen URI rather than the default WordPress location.

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.

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

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>

本文标签: url rewritingUse subdomain for certain urls