Skip to content
January 22, 2011 / Samer Baydoun

Fixing CPanel problem in redirecting domains to a URL friendly website

On one of the websites on an Apache server, I wanted to redirect two additional domain names to the main one, while using rewriting engine for URL friendly feature. I tried to use for that  the Redirect tool of CPanel with the Wild Card Redirect check. But like anything in a developer’s working life, it doesn’t work as one needs. The two domains didn’t work, with a “Server Not Found” error.

Back to the .htaccess file that the Cpanel modified I have found that the redirecting code is inserted after my friendly URL code:

RewriteEngine on
#URL Friendly
RewriteRule ^js/ - [L]
RewriteRule !\.(js|css|htm|gif|jpg|png)$ index.php

#Redirecting first domain
RewriteCond %{HTTP_HOST} ^firstDomaing.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.firstDomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.targetDomain\.com$1" [R=301,L]

#Redirecting second domain
RewriteCond %{HTTP_HOST} ^secondDomain.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.secondDomain.net$
RewriteRule ^(.*)$ "http\:\/\/www\.targetDomain\.com$1" [R=301,L]

Putting the URL friendly code after the redirecting code solved the problem. But wait, it still gives the “Server Not found” error when referring to a sub-directory at one of the redirected domains. To fix this I just needed to add \/? at the beginning of the redirecting RewriteRule so that it redirects the sub-directory to the root directory of the target domain.

But what if I need to redirect a sub-directory of the redirected domain to the same path at the target domain. To do it, I replaced $1 with %{REQUEST_URI} at the end of the same line. And with some refinement, I’ve got the code working properly:

RewriteEngine on

#Redirecting domains
RewriteCond %{HTTP_HOST} ^firstDomaing.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.firstDomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^secondDomain.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.secondDomain.net$
RewriteRule ^\/?(.*)$ "http\:\/\/www\.targetDomain\.com%{REQUEST_URI}" [R=301,L]

#URL Friendly
RewriteRule ^js/ - [L]
RewriteRule !\.(js|css|htm|gif|jpg|png)$ index.php
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.