Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Having trouble writing this compound rewrite containing the following rules:

Requirements:

E.G.

http://example.com/checkout/123 -> https://wwws.example.com/checkout/123 or http://example.com/checkout ->https://wwws.example.com/checkout/123

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: http://wwws.example.com it redirects that to http://www.wwws.example.com.

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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Don't use HTTP_HOST:

RewriteCond %{HTTP_HOST} ^!example.com$ [NC]  
RewriteRule ^(.*)$ http://www.example.com/$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>  
ServerAlias example.com  
RedirectMatch permanent ^/(.*) http://www.example.com/$1  
</VirtualHost>

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]

Or:

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
add comment

Your Answer

 
discard

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.