【发布时间】:2014-02-02 12:54:18
【问题描述】:
我想删除 www 并直接指向域的非 www 版本,经过一些研究,我发现下面这两行可以完成这项工作:
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
但是,当我将以上两行添加到下面的当前 .htaccess 文件中时,是否需要将 domain.com 位更改为网站域?如果是这样,我该如何更改它以便它自己知道域名?所以我不必手动更改它。
我发现这个article 可能是这个问题的答案,但我只是不知道如何实施它我不是真正的专家,但我知道一个错误会破坏一切,请帮助。
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]
【问题讨论】:
标签: php apache .htaccess mod-rewrite