Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Having trouble writing this compound rewrite containing the following rules:


E.G. -> or ->

What i have currently done:

I currently have a series of rewrites that rewrites the non-www to the www:

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

This is problematic because when a http request for the URI: it redirects that to

How would i achieve a rewrite that would satisfied my requirements?

share|improve this question
up vote 1 down vote accepted

Don't use HTTP_HOST:

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

My preference is for this:

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

Or much simplier:

<VirtualHost *:80>  
RedirectMatch permanent ^/(.*)$1  

Exercice left to the read: find more other ways.

To redirect https to http:

RewriteCond %{SERVER_PORT} !^80$  
RewriteRule ^/(.*) http://%{SERVER_NAME}/$1 [L,R]


RewriteCond %{HTTP} off  
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
share|improve this answer
Which is the most simple manner of doing so? – chrisjlee Apr 26 '12 at 20:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.