knightrider
knightrider

Reputation: 2583

Force www to directory using .htaccess

I have this application directory

http://www.example.com/application

When user browse without www, (e.g. example.com/application), i want to force and redirect them to http://www.example.com/application

How can i achieve using .htaccess.

Thank you.

Upvotes: 1

Views: 1598

Answers (1)

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

To redirect non www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

To redirect www to non www, the code is similar:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

Upvotes: 1

Related Questions