Having trouble writing this compound rewrite containing the following rules:



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?


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  

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}
  • Which is the most simple manner of doing so? – chrisjlee Apr 26 '12 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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