0

I need help to set up my .htaccess so it works with two different conditions.

The first condition is :

RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$  index.php?a=$1&q=$3    [L]

I want to add a condition that rewrites non-www to www

How do I force www. on my site?

I want to force www through htaccess, but I don't how to to this.

1 Answer 1

1

Any canonical redirect should come before your existing internal rewrites. So, try the following:

RewriteEngine On

# Canonical non-www to www redirect
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Internal rewrites
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]

I've also tidied your existing rewrites slightly. If you don't want to change the URL then use a single hyphen (-) in the substitution, rather than rewriting to itself. A pattern such as ([^/]*)+ (0 or more times, at least 1 time) is the same as [^/]+ (1 or more).

0

You must log in to answer this question.

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