Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard domain redirect with WordPress

I have two domain names setup with sub-domains as follows...

blog.domain.com
www.blog.domain.com

blog.domain.info
www.blog.domain.info

Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)

To keep Google happy, I want everything to redirect to this one domain...

blog.domain.com

Here is what's inside the .htaccess file contained in the WordPress directory...

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

So I went into my cPanel and added a 301 Domain Redirect as follows...

blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).

cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...

RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]

The problem is that the wildcard portion does not seem to work.

When I go to blog.domain.info, I get redirected to blog.domain.com as expected.

But when I go to blog.domain.info/my-post, I do not get redirected at all.

  1. How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.

  2. Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.

Thank-you!

like image 245
Sparky Avatar asked Apr 17 '11 00:04

Sparky


2 Answers

Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.

This all seems to be working.

Any comments appreciated.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

EDIT

It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
like image 65
Sparky Avatar answered Nov 07 '22 07:11

Sparky


i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.

for example, http://methodshop.NET/games/play/bubblewrap/index.shtml

would redirect to http://methodshop.COM/games/play/bubblewrap/index.shtml

here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.

RewriteEngine on

RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]

like image 45
jon Avatar answered Nov 07 '22 07:11

jon