Search code examples
apache.htaccesshttp-redirectmod-rewriteno-www

htaccess redirect www to non-www without changing in address


I have this address

http://name.com/category/1/news

I use this code in htaccess to force open page without www.

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

When I open http://www.name.com/category/1/news then I redirect to

http://name.com/category.php?id=1&slug=news

Here is my full htaccess file.

RewriteEngine on
RewriteRule ^news/(.*)$ news.php?id=$1&slug=$2
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Thanks.


Solution

  • Try :

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [NE,R=301,L]
    RewriteRule ^news/(.*)$ news.php?id=$1&slug=$2 [NC,L]