Forum Moderators: phranque
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...
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.
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]
# 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.
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]+¦\.)$
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]
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.