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

时间:2018-11-08 10:57:31

标签: .htaccess

我的.htaccess文件中有以下内容要发送给

  • 删除www
  • 重定向到https
  • 删除.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)。没有其他重定向有效

谁能发现我要去哪里错了?

2 个答案:

答案 0 :(得分:-1)

您似乎需要在.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 - Remove www, force https, remove php and remove trailing slash

答案 1 :(得分:-1)

在.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]