homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
301 redirection using .htaccess problems
angelsp




msg:4583156
 3:57 pm on Jun 11, 2013 (gmt 0)

We all know we can use .htaccess to 301 a website abc.com to cde.com

Now my situation is:

I pucahsed a shared hosting, /public_html/ is abc.com 's root, /public_html/www.cde.com/ is cde.com's root folder.

Whenever I changed .htacess of abc.com 's, it will affect cde.com.

for example, I put the following code to /public_html/.htaccess to redirect all the urls from abc.com to cde.com, but after I do so, when I type abc.com/test.php, it redirects to cde.com/test.php no problem, but gives a 404 error, for now all the links from cde.com is dead and unacessable. When i delete the following code from .htacess, cde.com works fine again.

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://cde.com/


My question is, how to change /public_html/.htaccess to redirect abc.com's url's to cde.com and don't affect cde.com website running.


Thank you for yoru help.

 

Readie




msg:4583178
 4:53 pm on Jun 11, 2013 (gmt 0)

You'd want to add a condition to your rewrite rule.

Something like this:

RewriteCond %{HTTP_HOST} !^cde\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]

Bear in mind, that any subsequent domains you attach to this file structure will be similarly 301'd. If that was undesired, you would need to modify the condition so that is only 301'd from abc.com
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]
Or you would need to add additional exclusionary rules for the new domains:
RewriteCond %{HTTP_HOST} !^cde\.com$
RewriteCond %{HTTP_HOST} !^(www\.)?efg\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]

Also of note - the first and third solutions in this post will force requests to
http://www.cde.com to instead go to http://cde.com, which prevents a potential problem with duplicate content arising.
lucy24




msg:4583243
 8:03 pm on Jun 11, 2013 (gmt 0)

Do your additional domains live in subdirectories inside the first domain's directory? Ugh. Out of your control, I know, but it's definitely easier when all domains are parallel so each can have its own htaccess without worrying about the others.

You've got at least two different issues:

#1 any rule specific to a single domain needs a preceding RewriteCond that says either

RewriteCond %{HTTP_HOST} !abc\.com

or

RewriteCond %{HTTP_HOST} def\.com

whichever is more convenient. Note that you have to leave off the opening anchor because the rule has to cover requests with or without leading www and maybe even subdomains.

#2 domain-name canonicalization. Here each rule needs two conditions, like this:

RewriteCond %{HTTP_HOST} example\.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

And then there's

#3 well, I did say "at least two" the all-purpose "index.html" redirect, which will be the same on all domains everywhere. Here it is tempting to capture the %{HTTP_HOST} and simply recycle it because then you don't need to write out a separate rule for each domain. But if you do this, you risk having to do two separate redirects if the request was for
example.com/index.php
and you need to redirect to
www.example.com/
alone.

You may be similarly tempted to take any rules specific to the new domains and make a new htaccess in their individual directories. Unfortunately this gets VERY messy if you have RewriteRules in both places. The two options are:

RewriteOptions inherit
= RewriteRules in the outer htaccess will be put on hold while the second htaccess is checked, and will only kick in if not superseded by something in the inner htaccess

(no RewriteOptions inherit)
= RewriteRules in the outer htaccess will simply be IGNORED; only the inner htaccess will apply

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved