Mohammed Sufian
Mohammed Sufian

Reputation: 1781

issue in url rewrite in .htaccess

i am trying to force non-www to www and its working as aspected:

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

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

now if i visit www.example.com/registration it works great.. but when i am accessing the same url from an link it works but adds index.php?/ in the url like this:

www.example.com/index.php?/registration

i am new to .htaccess programming.. i can understand i need ot modify the below lines.. but i dont know how to modify in such a way so that i does not add index.php?/ in the url...

  RewriteRule ^(.*)$ index.php?/$1 [L]
  RewriteRule (.*) http://www.example.com/$1 [R=301,L]

any help or suggestion would be great help.. thanks in advance

Upvotes: 1

Views: 32

Answers (1)

anubhava
anubhava

Reputation: 785128

Swap the order of your rules:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]

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

Upvotes: 1

Related Questions