Ayush
Ayush

Reputation: 42450

Adding a rewrite rule to my existing rules in htaccess

My current htaccess file is this:

RewriteEngine On
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ /index.php?$1 [L]

I want to add another rule that redirects www.example.com to http://example.com with a 301 Redirect. I know the rule I need to add is

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

But where do I add this? Do I append this to the end of my current file, or do all Conditions go together, and then all rules together?

Upvotes: 0

Views: 74

Answers (1)

Hardik Raval
Hardik Raval

Reputation: 1940

Yes you can append , [L] represent the last rule , so first make it your last rule after use redirect [L,R=301]

Use following code in your case

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

Upvotes: 5

Related Questions