.htaccess重定向以删除php并重定向到https和非www



我在的.htaccess文件中有以下内容

  • 删除www
  • 重定向到https
  • remove.php

这是我的.htaccess:

# ----------------------------------------------------------------------
# | Redirect to HTTPS & remove www                                     |
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301] 
# ----------------------------------------------------------------------
# | Remove .php extension                                              |
# ----------------------------------------------------------------------
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

当我在Firefox中查看该网站时,我会收到以下错误:

页面重定向不正确。此问题有时可能是由禁用或拒绝接受cookie 引起的

Chrome警告重定向过多

URL更新为我所期望的(删除www并添加https(。没有其他有效的重定向

有人能认出我哪里错了吗?

在.htaccess文件中似乎需要以下内容:

# ----------------------------------------------------------------------
# | Redirect to HTTPS & remove www                                     |
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [OR]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# ----------------------------------------------------------------------
# | Remove .php extension                                              |
# ----------------------------------------------------------------------
RewriteRule ^(.+).php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]

这是借用了以下类似主题的公认答案:

.htaccess-删除www,强制https,删除php并删除尾部斜杠

在.htaccess文件中尝试以下代码:

RewritEengine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

最新更新