Apache: Rewrite to WWW

I have some questions about this old code…

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

Q1.) Could I change the RewriteCond to…

RewriteCond %{HTTP_HOST} !^www\. [NC]

Q2.) What does the RewriteRule “Pattern” consist of? Is it always %{THE_REQUEST}, which is the part after the domain name?

Q3.) If you have one or more RewriteCond, then is it okay to make the “pattern” in the RewriteRule like this…

RewriteRule ^(.*)$ 

The logic being that if you have a “Condition(s)” in the RewriteCond’s then maybe you don’t need to specify all of that in the RewriteRule’s “pattern”?

Hmmm… I truly dislike the rewrite code. It is confusing to all !
I am not the strongest on this subject, but, will attempt to help you understand it further.

So, first, there are to main parts. Actually several others, but in your example two parts.

The first is the “Condition” … The condition can be just about anything. Usually, it is either based on the HTTP_HOST, REQUEST_FILENAME, QUERY_STRING or the HTTP_URI options although there are others out there. Each of these are used for various parts of the header string. HOST would be for example if you wanted everything on the site to change from HTTP into HTTPS. FILENAME would be used to alter the displayed filename. ETC…

The next part is the RULE section. This tells the server how to handle the rewrite itself. Sometimes it can be confusing as the code is quite cryptic!

So, Q1 … They are NOT the same command. The original says it is looking for a domain name with any type of extension. So, it checks for .org, .com, .net, .etc… The second version just looks for the www. So the answer is yes, you can write that, but, it totally depends on what you want it to do. So, for example, if you want to force all URLs to use the www, in other words ernie.com becomes www.ernie.com, you can use this:

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

To do the opposite, to remove the www from all of the URL’s, you can use this:

        RewriteCond %{HTTP_HOST} ^www\.example\.com$
        RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

So, your change to !^www. would make it check for NOT starting with www. If that is what you want, then, yes, you can do that. Normally you want to force a user to stick to your displayed versions. But, I think you need to add the $ at the end of it so it knows it is a string to use in the Rule…

Q2: Well, basically yes. The RULE is set up like this: RewriteRule PATTERN TARGET [FLAGS]
The pattern is a REGEX formula that pulls apart the parts of the URL. The target is what you want out of that data as a results. The flags are used inside the headers for various reasons like sending out a header-return-code like a 301 or other special codes. See my note below about Apache’s docs.

Q3: You can have several Cond’s in a rewrite. But, the Rule MUST match each of the conditions. So the example shown above writing the www.-host-.rest of line which would force the “www.” to be shown on each and every page would not be the same as the Q3 example. Not completely sure what the question is here.

So, perhaps you should study up the Apache’s docs on this which are fairly easy to read. Here is a link to them. It is the “beginner’s” version, so it explains it in a simple intro way. I think it will be good for you to read it and might help solve your questions. https://httpd.apache.org/docs/2.4/rewrite/intro.html

But, if you still have questions ask away… I have found that if you have a test server set up on your computer, it is easy to just experiment. Since you should have one set up outside of your live site environment, it makes it very easy to test locally. Just rewrite the “rewrite”, save and refresh your browser.
Do you have Wamp or Mamp installed? Just a thought… Free local server, MySQL, and PHP all in one simple download.

Hope this helps a bit…

Sponsor our Newsletter | Privacy Policy | Terms of Service