Vijay G
Vijay G

Reputation: 63

.htaccess rule not working correctly (non-www to www redirect)

Here is format of my URLs: www.domainname.com/in/laptop.html

In .htaccess I have set to call search.php?country=in&source=seo_url&keyword=laptop.html

Rule used:

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-l
 RewriteRule ^([\w]+)/([\w-]+).html$ search.php?country=$1&source=seo_url&keyword=$2.html

Now issue is if someone call domainname.com/in/laptop.html (without www), it does not show any product

Then I used below rule to redirect non-www to www URL

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

What is happening: When I call domainname.com/in/laptop.html , it redirect to below URL

http://www.domainname.com/search.php/laptops.html?country=in&source=seo_url&keyword=laptops.html

What It should do: if call domainname.com/in/laptop.html , it should redirect to www.domainname.com/in/laptop.html

Upvotes: 0

Views: 56

Answers (1)

Croises
Croises

Reputation: 18671

Redirect to www before rewrite:

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([\w]+)/([\w-]+).html$ search.php?country=$1&source=seo_url&keyword=$2.html

Upvotes: 1

Related Questions