homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess for a domain with multiple aliases. can .htaccess do this?
different domain rewrites.

 3:28 pm on Aug 9, 2011 (gmt 0)

I have about 7 of these in my .htaccess file

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

The problem is when I go here [ragepank.com...] it says that www and non www both load for them (I know on my end it's not true).

So I looked at the code they recommend and it's a little different:

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

The problem with their code is it takes sites like otherwebsite.com and points them to website.com and that's not good.

Is there a way with wildcards to do something like this?
RewriteCond %{HTTP_HOST} !^www\.(.*)\.com
RewriteRule (.*)/(.*) http://www.$1.com/$2 [R=301,L]

I've tried that code I believe and it didn't work.

This is the other code I want to wildcard :

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.website.com/$1 [R=301,L]



 8:23 pm on Aug 9, 2011 (gmt 0)

The generic way to allow with-and-without input is
where you're not capturing the www. but parenthesizing to keep it as a unit.

One obvious question: Is your host doing anything about the with-and-without-www element? If so, one version will be redirected to the other before it ever reaches your htaccess. (If you were doing it yourself, it would be at the very end of the .htaccess.) You can see it most easily in your raw logs, where some visitors will be inexplicably redirected before getting the door slammed in their face.


 8:33 pm on Aug 9, 2011 (gmt 0)

Never use (.*) at the beginning or in the middle of the RegEx pattern.
The (.*) means "grab all of the input".

You'll need two rulesets to do the job properly.

One will redirect non-www requests to www. The other will redirect www requests with appended port numbers or periods to the www version with the extra stuff removed.

Patterns for your RewriteCond:

!^www\. - does not begin www.
^([^.]+\.com)$ - captures domain name

!^(www\.[^.]+\.com)$ - not "exactly" www.example.com
^(www\.[^.]+\.com) - captures domain name

[edited by: g1smd at 8:33 pm (utc) on Aug 9, 2011]


 8:33 pm on Aug 9, 2011 (gmt 0)

It's taken care of in the htaccess file only. I will make those changes you said, but my question about wildcards still stands. The .htaccess file is serving about 8 domains. It's a Drupal multi site installation.

Could I change:


EDIT, I posted this before the second reply loaded for me.


 8:34 pm on Aug 9, 2011 (gmt 0)

Never use (.*) at the beginning or in the middle of the RegEx pattern.

The (.*) means "grab all of the input".

The parser then has to make tes of thousands of trial match pattern attempts to find out what you actually meant.


 8:39 pm on Aug 9, 2011 (gmt 0)

So, the code g1smd:

!^www\. - does not begin www.
^([^.]+\.com)$ - captures domain name

!^(www\.[^.]+\.com)$ - not "exactly" www.example.com
^(www\.[^.]+\.com) - captures domain name

Thanks for breaking it down! I am still learning, but trying to see if I get how I would change the output.

RewriteCond %{HTTP_HOST} !^(www\.[^.]+\.com)$

RewriteRule !^(www\.[^.]+\.com)$/(*.) [$1...] [R=301,L]

I don't think this is right, but I tried to put your code where I thought it fit best.


 9:46 pm on Aug 9, 2011 (gmt 0)

You'll need two rules and each rule will need TWO conditions, each using a
RewriteCond looking at %{HTTP_HOST}, in order to do the right thing.

The first ruleset needs to redirect all non-www requests.

The second ruleset needs to redirect www requests that have an appended port number and/or appended literal period.

RewriteRule RegEx pattern can match only the path part of the URL request, so use (.*) here to redirect all URLs.

Both rules need the
[R=301,L] flag.

Use example.com here in the forum to stop forum auto-linking.

[edited by: g1smd at 9:57 pm (utc) on Aug 9, 2011]


 9:51 pm on Aug 9, 2011 (gmt 0)

I understand that I'll need two rules, but creating those rules is what I'm not getting.
you broke down your help in to 4 lines, but taking what you've given me, and converting it into what I'm after is where I'm lost.

I understand what you're saying:

this is rule one, redirect all non 'www' tp 'www'
contidion 1
condition 2

this is rule two send index.php to /
contidion 1
condition 2

but with my limited knowledge, I can't create those rules from what you gave me.


 9:52 pm on Aug 9, 2011 (gmt 0)

g1smd, I tried to sticky mail you the following:

Thanks for taking the time to help me with my htaccess issue!

But it said your mailbox was full. Just want you to know that I appreciate the help.


 9:56 pm on Aug 9, 2011 (gmt 0)

No. If you want to redirect index.php to / then that will involve a third rule.

This index redirect will need to be listed first in the .htaccess file so that non-www index requests do not create an unwanted multiple step redirection chain.

The next two rules need to redirect
1. all non-www, and
2. www with port or period.

These two
RewriteRules will each have two preceding RewriteCond lines looking at %{HTTP_HOST}, using the four patterns discussed above.

 10:47 pm on Aug 9, 2011 (gmt 0)

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

#1 There exist situations where you begin a rule with ! but they are rare. Yours is not one of them.

#2 $ in the pattern means "the end of the input" so you would never have it in the middle.

#3 (*.) is presumably a typo.

#4 most important, RewriteRules look only at the middle part of the request-- after the domain name, before the query if any. So it would never begin with example.com anyway. The part you've given as $1 would actually be %1 where you've captured the domain name from one of the conditions.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
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