1

How do I extend htaccess file to do .domain.com -> www.domain.com?

  <IfModule mod_rewrite.c>
        RewriteEngine On
        #catch potential subpages first
        RewriteRule ^([a-z-]+)/([0-9a-z-]+)/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]
        #catch parent page without subpage
        RewriteRule ^([a-z-]+)/?$ index.php?page=$1 [L,NC,QSA]
  </IfModule>

Is it possible to not define domain.com? and have generic rule for any domain?

Normally I would do like this:

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

But how do I unite it with directives above?

1 Answers1

1

Have your rules like this:

RewriteEngine On

# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]

#catch potential subpages first
RewriteRule ^([a-z-]+)/([0-9a-z-]+)/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]

#catch parent page without subpage
RewriteRule ^([a-z-]+)/?$ index.php?page=$1 [L,NC,QSA]

PS: If you want to add www for subdomains also then use this rule:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643