PDA

View Full Version : rewrite rules - excluding subdomains


the_pm
May 18th, 2007, 19:39
Hey everyone,

I'm having a little bit of trouble with a rewrite I'm attempting to do. I'm looking to redirect a non-www URL over to the www version, but without affecting subdomains. The first part is easy. The second part is proving to be a bit of a challenge for me (my Apache scripting knowlegde is not great).

So far, I have:RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]Works great, except subdomains are redirecting. This is bad! I've tried a few things, but so far nothing's worked. My latest attempt was (pretend ignoreme is a subdomain):RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{REQUEST_URI} !ignoreme/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]Since the subdomain is actually a folder within the site root, I thought this might work. Obviously it didn't. Any ideas what might work here?

kirxklox
May 18th, 2007, 21:51
Why are you worried about the Sub-Domains. All you are rewriting are the folders, upon completion delete the extra folders?

the_pm
May 18th, 2007, 22:08
I'm worried about subdomains because I'm rewriting at the root level down through the entire site, and subdomains are actually folders within the site structure, and therefore subject to the .htaccess markup above. I'm trying to either exclude a list of folders, or set up the markup so it understands not to rewrite anything presented as a subdomain.

I took a look at rewritebase thinking the answer might be there, but I couldn't see how to apply it.

inimino
May 19th, 2007, 00:57
Hi Paul,

Instead of matching the negation of www.example.com, you need to match what you actually want, which is example.com alone.

(REQUEST_URI is the URI in the actual HTTP request, which knows nothing about your local directory structure, which is why that isn't working, but excluding each subdomain is ugly anyway.)

kirxklox
May 19th, 2007, 01:06
Paul: A sub-domain is telling the internet and the server how to handle a Folder at the Root.

http://whmb.webhorology.com/
and
http://www.webhorology.com/whmb/

are both valid URLs

the_pm
May 20th, 2007, 02:08
Paul: A sub-domain is telling the internet and the server how to handle a Folder at the Root.

http://whmb.webhorology.com/
and
http://www.webhorology.com/whmb/

are both valid URLsYes Sam. It is because the second one is also valid that my markup was affecting it. Now you see the problem ;)

Thank Mj. I'll change that one line :)

kirxklox
May 20th, 2007, 03:06
That is the reason I quit using Sub-Domains two years ago.

Dan
May 20th, 2007, 03:58
Maybe a wildcard (*) instead of www in the rewritecond?

kirxklox
May 20th, 2007, 05:20
Paul: The only suggestion I can come up with is a While statement to include both identities if there is such an animal, or ignoreboth.

kirxklox
May 20th, 2007, 05:32
the_pm: Have you tried changing the Permissions around to allow the Sub-Domains to Ignor the Rewrite?

the_pm
May 21st, 2007, 16:32
Sorry, I should have mentioned changing the condition worked great. Instead of testing for the non-existence of a string, I tested for its existence. Predictably enough, it worked. I get the DUH stamp across my forehead for that one. Rookie mistake :oops:

Here's the functioning code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]