Forum Moderators: phranque

Message Too Old, No Replies

.htaccess force www multiple domains from cPanel root

code to ensure all add-on domains include www

         

loveunit

3:34 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



Hi, I'm sure parts of this have been covered, I've searched loads of articles and put together a code, but I'd like to make it more generic, so that I do not have to place a copy in the root folder of each add-on domain, working only with the .htaccess in the root of public_html.

I'd like a generic code to add www to all domains, however there are sub-domains, which I'd like to exclude with a rule.. here is where I'm at:

# if requested hostname does NOT start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# and if hostnames not no-rewrite.com or not-rewrite.com then get it to %1 variable
RewriteCond %{HTTP_HOST} !^(no-rewrite\.com¦not-rewrite\.com)
# prepend "www." to requested hostname and file
RewriteRule (.*) [%1...] [R=301,L]

If it has changed, it was a full pipe in between the domains in the second rule.

all my sites run from sub directories, so I have the following to redirect the cPanel default domain, this is one of the domains that uses sub-domains.

# www.no-rewrite.com

RewriteCond %{HTTP_HOST} ^www.no-rewrite.com [OR]

RewriteCond %{HTTP_HOST} ^no-rewrite.com

RewriteCond %{REQUEST_URI} !^/folder/

RewriteRule ^(.*)$ folder/$1 [L]

I'm not sure to put this above or below the more generic code..

I'm having troubles testing also, as I keep breaking all my clients sites - opps!

the results of running this code is that when I try no-rewrite.com the browser kicks it on to www..com, trying it on any other site seems to break the forwards set-up by cPanel, as the end result is:

[.com...]

where cpanel/formward = the directory I have pointed the domain to when adding it to cPanel.

hmm, perhaps this is more complex than I thought - any ideas, TIA...

loveunit

3:38 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



I see I wrote ww instead of www as title, any way to edit that?

g1smd

6:38 pm on Oct 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Redirects should always be listed before rewrites in the .htaccess file.

The [OR] can be simplified using

[b]([/b]www\.[b])?[/b]domain\.com

You can edit posts for a few minutes after initial posting by using the edit link under your user name on the post.

Posting in the forum modifies the pipe symbols.

Note that

^(.*)$
can be simplified to
(.*)
too.

jdMorgan

7:10 pm on Oct 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe another important cause of the problem is that you cannot back-reference a negative-match pattern. This is because when the RewriteCond is true (the pattern matches), then what you want to back-reference is NOT matched into the pattern, and therefore, not available for back-reference.

This ought to work better:


# if requested hostname does NOT start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# and if hostnames not no-rewrite.com or not-rewrite.com
RewriteCond %{HTTP_HOST} !^(no-rewrite¦not-rewrite)\.com
# and if non-blank, get requested hostname (exclusive of trailing dot and/or port number) to %1 and
RewriteCond %{HTTP_HOST} ^(.+)(\.?:[0-9]+¦\.)$
# prepend "www." to requested hostname and file
RewriteRule (.*) http://www.%1/$1 [R=301,L]

Jim

loveunit

7:42 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



so, I'll try this when everyones gone to sleep tonight, but I wonder if I post it someone will have time to give it a look over first?

# redirect main cPanel domain to sub-directory
RewriteCond %{HTTP_HOST} ^(www\.)?main-domain\.com
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) folder/$1 [L]

# if requested hostname does NOT start with "www."
RewriteCond %{HTTP_HOST} !^www\.
# and if hostnames not sub.main-domain.com or sub.other-main-domain.com
RewriteCond %{HTTP_HOST} !^(sub.main-domain¦sub.other.main-domain)\.com
# and if non-blank, get requested hostname (exclusive of trailing dot and/or port number) to %1 and
RewriteCond %{HTTP_HOST} ^(.+)(\.?:[0-9]+¦\.)$
# prepend "www." to requested hostname and file
RewriteRule (.*) [%1...] [R=301,L]

the hostnames I want not to add www to are both sub-domains or two seperate domains - sub.main-domain.com & sub.other-main-domain.com

I'm not sure how cPanel do their add-on domains, I guess it uses apache redirects? ( stab in dark, as I know nothing of this ), my question would be is it possible that the .htaccess and cPanel code could cause conflicts?

thanks again for all the help and advice.

loveunit

11:02 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



odd - I've tried and the result was, well, nothing - no faults to report and no affect.

the issue might be in this line:

RewriteCond %{HTTP_HOST} ^([a-z]+\.[a-z]+)

if I swap it to the following, it works on a single domains, but when I add the code from my last post to the root, nada!

any suggestions?

RewriteCond %{HTTP_HOST} ^(.+)(\.?:[0-9]+¦\.)$

g1smd

11:21 pm on Oct 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The "working" line matches "some characters of any type (letters, numbers, punctuation, whatever), at least one, followed by a trailing dot and/or port number". That line is all about removing junk from the end.

The "faulty" line matches one or more A to Z characters followed by a dot, followed by one or more A to Z characters.

So, it would fail if there is a hyphenated domain name or somesuch involved.

You need to take a basic code suggestion and craft it for your exact circumstance.

The rules would also be different if you domain was example.co.uk instead of example.com - the former has one more element.

[edited by: g1smd at 11:30 pm (utc) on Oct. 26, 2008]

g1smd

11:29 pm on Oct 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The first thing that you call a redirect is in fact a rewrite. Change your note so that you too call it a rewrite.

You have that rewrite coded as first. Rewrites should always be placed after redirects.

The way the rewrite is coded, you are exposing every page of your site to being indexed both as a www URL and again as a non-www URL.

You should have an additional site-wide non-www to www redirect coded before that rewrite. That will fix the issue.

loveunit

6:23 pm on Oct 27, 2008 (gmt 0)

10+ Year Member



thanks for the advice, I've tried a few more things and I'm still getting errors, I need to get on with a project but will return to this topic - hopefully with working code soon.