URL prefix rewrite problem

Hi. I am trying to update my .htaccess file to rewrite URLs to meet the following requirements:

  1. If a user types in myurl.com instead of www.myurl.com, I want to rewrite the url as www.myurl.com.

  2. I also want the user to be able to go to one of my virtual domains like: support.myurl.com or intranet.myurl.com.

I currently have a solution to #1, but it is rewriting my virtual domains for #2 as www.

Does anyone know how to modify my code to accommodate both requirements?

Here is my code:

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /

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

</IfModule> 

wm,

That’s covered as one of the examples in my signature’s tutorial article.

Also, if you’re the webmaster for your site, don’t you know whether mod_rewrite’s enabled or not? In other words, only use <IfModule> blocks if you’re developing for others who may not have mod_rewrite enabled on their servers (because it could cause an endless series of 500 errors) but NEVER make Apache check on your own server - try to speed things up rather than slowing.

Ditto the RewriteBase. If you’re not redirecting (via mod_alias), it has NO PLACE in your mod_rewrite code.

Regards,

DK

DK,
Thanks for the advice. I went to your tutorial, which is great by the way. The examples came close to what I’m looking for, but example 1 puts the www in front of the subdomain.

I want urls without a subdomain to be enforced to be a www.mydomain.com domain. Urls with a subdomain should not have the www in front of the domain: e.g. intranet.mydomain.com

How do I tweak your example to make that change?

wm,

You go back to example 1.

Okay, it’s a little fancier than just enforcing www but that is occasionally needed, too. Remove the first RewriteCond if you prefer.

Regards,

DK

DK,
I think I have not made my requirements clear enough. I do not want subdomians to have a www in front of them. I want subdomains to look like:

http://support.mydomain.com

and not like:

http://www.support.mydomain.com

However, I do want my root domain:

http://mydomain.com

to be redirected to

http://www.mydomain.com

Your example works fine for the latter part, but it puts a www in front of my subdomain, which is what I don’t want.

Thanks,
Ken

wm,

That’s even simpler (and you had it to begin with - as long as you got rid of the @#$% that was with it)!

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

The No Case flag on the RewriteCond statement is because domain names are case insensitive and the /? after the regex start anchor is because Apache 1.x handles the {REQUEST_URI} sting differently than Apache 2.x.

Regards,

DK

DK,
The code you just gave me does not handle subdomains. When I type in:

http://support.mydomain.com

It gets rewritten as:

http://www.mydomain.com/support - The directory for the support virtual domain.

That code works fine for adding the www to the base domain, but that is what I started with and it has always worked fine, even with all the &#&$ that was with it. I need it to enforce www on the root domain AND allow the subdomains to go through without rewriting them or adding the www in front of the subdomain.

Your code is simply saying: “Does the URL have www in front of it?” I want it to say: “If the URL is not a subdomain, then does the URL have a www in front of it” If both conditions are true, then add a www in front of the base URL. If it is a subdomain, leave it as is.

Restated: If there is no subdomain (including www), redirect to www (which will ignore any subdomain because it does not match the condition of the RewriteCond).

RewriteEngine on
RewriteCond &#37;{HTTP_HOST} ^mydomain\\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.mydomain.com/$1 [R=301,L]

I hope I got it right this time and thanks for clarifying!

Regards,

DK

Works like a charm. Thanks!

wm,

Great! It’s all in the “specificity.”

Regards,

DK