Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

php - Detect the domain name automatically in .htaccess

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you only want to redirect www.example.com to example.com, you can simply match on www.. If you want to redirect every subdomain (which is usually not the case), you can possibly match and try to match if it has two parts. It is usually easier to just alter the example.com to be your own domain name. In any case, you should add the rule before any other rule.

#Option 1: Only redirect if it begins with www.
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#Option 2: Redirect all the things! (including thingy.sub.domain.example.com to example.com)
RewriteCond %{HTTP_HOST} ^.*.([^.]+.[^.]+)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...