Forum Moderators: phranque

Message Too Old, No Replies

.htaccess to redirect to https in all situations

RewriteCond to redirect http and https to https

         

mattlegr

8:19 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



Want to redirect any of the following entered URL's on Apache 2.x:
[leaders.mydomain.com...]
leaders.mydomain.com
[leaders.mydomain.com...]
www.leaders.mydomain.com
[leaders.mydomain.com...]

to always go to this url:

[mydomain.com...]

The below lines in my .htaccess and handle all of the above except when a person actually types out [leaders.mydomain.com....] I'm very new to the syntax and have groped my way to this point by borrowing from other examples, etc.

RewriteCond %{HTTP_HOST} ^leaders.mydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.leaders.mydomain.com$
RewriteRule ^(.*)$ [mydomain.com...] [R=301,L]

gergoe

8:57 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



That looks to be fine, redirects the browser to [mydomain.com...] if the requested domain is leaders.mydomain.com or www.leaders.mydomain.com regardless that it was requested using SSL or not. Although you could simplify a bit, like using slightly more complicated regular expressions:

# You'll need this anyway 
RewriteEngine On
# This will match on both leaders.mydomain.com and www.leaders.mydomain.com.
# Note that the closing anchor ($) is removed to make sure it will match even
# if the port number is defined in the request (then it becomes
# leaders.mydomain.com:80 for example.

RewriteCond %{HTTP_HOST} ^(www\.)?leaders\.mydomain\.com
# here I removed the brackets, which is used for grouping or capturing
# back-references, but you don't need any of them

RewriteRule .* [mydomain.com...] [R=301,L]

That's it.