1

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?

1 Answer 1

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .