Forum Moderators: phranque
Options +FollowSymLinks
RewriteEngine On
#
RewriteCond %{REQUEST_METHOD} !^(GET¦POST¦HEAD)$
RewriteRule .* - [F]
#
# http://example.com - http://www.example.com
RewriteCond %{HTTPS} =off
REwriteCond %{HTTP_HOST} =example.com
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
#
# https://example.com - https://www.example.com
RewriteCond %{HTTPS} =on
REwriteCond %{HTTP_HOST} =example.com
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
It was working like a charm.
But then due to some Complaince, I had to turn off SSLv2 in httpd.conf
and added following lines to the httpd.conf
SSLProtocol -ALL +SSLv3 +TLSv1
SSLCipherSuite ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM
But now my htaccess redirect for
[example.com...] to [example.com...]
is not working.
For some reason,
RewriteCond %{HTTPS} =on
does not work in any case.
Hence,
http://example.com redirects properly to http://www.example.com
but
[example.com...] does not redirects to [example.com...]
Please reply back with any changes I need to do to my htaccess code to get it working again.
It is very important for me as the SSL certificate is a UCC and covers only www and not non-www.
Thanks
[edited by: jdMorgan at 4:04 pm (utc) on July 5, 2009]
[edit reason] example.com, formatting [/edit]
# http://example.com - http://www.example.com
RewriteCond %{HTTPS} =off
REwriteCond %{HTTP_HOST} =example.com
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
#
# https://example.com - https://www.example.com
RewriteCond %{HTTPS} =on
REwriteCond %{HTTP_HOST} =example.com
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L][code]
# Externally redirect example.com to www.example.com, preserving HTTP/HTTPS protocol
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
Flush your browser cache before testing any new code uploaded to your server.
If you don't use any subdomains other than "www" and you have no plans to do so, then consider replacing the hostname test RewriteCond with:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ Jim
I saw you helped someone with the opposite problem with:
With a (slightly-optimized and more-robust) rule such as:
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com
RewriteRule ^(.*)$ [domainB.com...] [R=301,L]
Redirecting domainA to domainB
Can you help me reverse that rule?
If anything helps, here is the website:
and here is the current htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_METHOD} (TRACE¦TRACK)
RewriteRule .* - [F,L]
RewriteCond %{REQUEST_METHOD} !^(GET¦POST¦HEAD)$
RewriteRule .* - [F]
# Externally redirect example.com to example.com,preserving HTTP/HTTPS protocol
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
It works perfectly for http but when it comes to https, I get the error
'ssl_error_bad_cert_domain'
Dear jdMorgan,
i used your stuff:
# Externally redirect example.com to www.example.com, preserving HTTP/HTTPS protocol
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
in order to accomplish the same redirection as the topic author ( pranaysharmadelhi)
It works nice in my case, too (when placed within an .htaccess file).
But i decided to put these configuration lines within httpd.conf instead of in .htaccess (because the Apache documentation recommends this).
For this to work, i changed the last line to:
RewriteRule ^(.*)$ http%2://www.example.com$1 [R=301,L]
(no slash between 'com' and '$1')
And the things work but only in http mode. When i request an https - no conditions are checked no rewrite is accomplished?
Any ideas why?
The difference between rules used in .htaccess and http.conf (or other) server config files is that in .htaccess or within a <Directory> section of a config file, the path 'seen' by a RewriteRule is localized to that directory, whereas in a config file outside of any <Directory> section, it is a full-URL path. So in the case of a client request for "example.com/<anything>" the RewriteRule pattern in .htaccess or within a config <Directory> section will *not* start with a slash, whereas within a config file but outside of a <Directory> section, it will.
So for example, in a config file:
<Directory />
# Inside config file directory section
RewriteRule [b]^p[/b]ath$ [b]n[/b]ew-path
</Directory>
#
# Not inside a directory section
RewriteRule [b]^/a[/b]nother-path$ [b]/a[/b]nother-new-path
RewriteRule ^path$ new-path
RewriteRule [b]^/([/b].*)$ http%2://www.example.co[b]m/$[/b]1 [R=301,L] RewriteRule [b]^/?([/b].*)$ http%2://www.example.co[b]m/$[/b]1 [R=301,L] Jim
Reading trough the Apache docs i found this:
...
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts.
...
here: [httpd.apache.org...]
So the things started to work when i added the following 2 lines to the virtual host configuration:
RewriteEngine On
RewriteOptions Inherit
Thanks for your support - also thanks for the suggestion about places where the rewrite conditions/rules can live.