user796443
user796443

Reputation:

mod rewrite from .domain to www.domain htaccess

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?

Upvotes: 1

Views: 94

Answers (1)

anubhava
anubhava

Reputation: 785038

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]

Upvotes: 1

Related Questions