0

How can I make it so that whenever a page on mydomain.com is accessed without www to go to www.mydomain.com, please?

Thank you.

1

1 Answer 1

2

Try this in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

# redirect for http
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]  
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?(.*)$ http://www.mydomain.com/$1 [R=301,QSA,L,NE]

# redirect for https
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]  
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?(.*)$ https://www.mydomain.com/$1 [R=301,QSA,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters

$1 is your REQUEST_URI

5
  • Your ^(.*)$ will capture the leading slash; you don't want to add a second in the location that you're redirecting to. Apr 10, 2011 at 22:29
  • I don't need the FollowSymLinks do I?
    – Francisc
    Apr 11, 2011 at 11:31
  • It's not 100% good yet. :) If I enter: mydomain.com/folder/ it will go to www.mydomain.com instead of www.mydomain.com/folder/.
    – Francisc
    Apr 11, 2011 at 11:36
  • Also, I would like it to keep https if that's how it came.
    – Francisc
    Apr 11, 2011 at 11:41
  • 1
    @Francisc: Please see my edited answer, that takes care of http and https both.
    – anubhava
    Apr 11, 2011 at 12:19

You must log in to answer this question.

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