I want to remove www and direct to the non www version of the domain, after some research I found that these two lines below will do the job:
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
However, When I add the above two lines to my current .htaccess file below, do I need to change the domain.com bit to the website domain? if so, how can I change it so it knows the domain name by it self? so I don't have to change it manually.
I found this article with MAYBE an answer to this question, but I just don't know how to implement it I am not really an expert but I know that one mistake and can destroy everything, please help.
DirectoryIndex index.html index.php
ErrorDocument 404 /404
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET (.*).php HTTP
RewriteRule (.*).php$ $1 [R=301,L]
# remove index
# By puting the L-flag here, the request gets redirected immediately
# The trailing slash is removed in a next request, so be efficient and
# dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]
# remove slash if not directory
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
# no such file exists. Be safe and add an extra condition
# There is no point in escaping a dot in a string
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|.php)$
RewriteRule (.*) $1.php [L]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…