Forum Moderators: phranque
I have tried oodles of things to no avail.
RewriteCond %{REQUEST_URI} [example\.com...]
RewriteRule (.*) [example.com...]
Among using HTTP_Host and that didn't work either. I tried using HTTP_Host to see if www did not exist, then redir to [example.com....] This works if they do http://example.com but does not work with https (https://example.com).
Can anyone help?
# Redirect https://example.com/<anything> to https://www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Redirect non-www to www, preserving http/https protocol
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
# Redirect all but canonical or blank hostname requests to www, preserving http/https protocol
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
Jim
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com$1 [R=301,L]
This works on port 80 but not on port 443. So if we use http://example.com it redirects to [example.com...] and if we use example.com it redirects to [expample.com....]
BUT if we use [example.com...] it does NOT redirect to [wexample.com....] Any further suggestions?
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ [example.com...] [R=301,L]
The above works if they do http://example.com
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ [example.com...] [R=301,L]
The above does NOT work if they do [example.com...] but I need it to.
Thanks again!
The code will need to go into a common directory, or you may need to copy it into the DocumentRoot directory .htaccess file of each server.
Jim
And again, if your setup defines two virtual servers, you'll need the code in both. However, if tht is the case, then the code won't need to test SERVER_PORT, because code for each vHost will only be run for that vHost.
Jim
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/(.*)$ [example.com...] [R=301,L]
Did not work. also tried:
#RewriteCond %{HTTP_HOST} ^https://example\.com
#RewriteRule ^(.*)$ [example.com...] [R=301,L]
Did not work.
The setup does not use virtual servers/hosts and it is the only domain.
You can pretty much stop "trying code" as all of the routines I posted are tested/working routines. With the leading slash on the pattern (which you forgot on your commented-out snippet above), any of them should work; the only difference between them is as-documented in the comments. But you've either got the code in the wrong place, or you need to duplicate it in the SSL and non-SSL 'setups.'
Jim
Be sure that all prerequisites for the use of mod_rewrite are met in both SSL and non-SSL cases:
If you're getting any relevant errors or warnings in your server error log, please post them.
Jim