HTACCESS 停止了删除页面扩展的工作

HTACCESS Stopped working for removing page extensions

我一直在使用 htaccess 来执行以下功能:

  1. 重定向到 https 版本
  2. 添加 WWW。如果请求没有 www
  3. 从所有页面中删除 .php 扩展名
  4. 为所有页面添加尾部斜杠(例如 /page/)

我使用的代码是:

RewriteRule ^(directory1|directory2)($|/) - [L]
ErrorDocument 404 /404/

#Remove Php extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/$ .php
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/([^/]+)/$ //.php

#Add Tailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/ [R=301,L]

#SEO INDEX FIX

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://www.example.com/ [R=301,L]

以前一切正常,但现在升级到 php 7.4 后,如果我尝试使用 .php 扩展访问页面,它不会删除 .php 并添加 /。

请帮我更正 htaccess 代码..

谢谢!

您可以使用这个 htaccess 文件:

RewriteEngine On
#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$  [NC]
RewriteRule (.*) https://www.%1/ [R=301,L]
#Add a trailing slash to pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [R,L]
 #Remove .PHP from URLs
 #The rule bellow redirects URLs to remove .PHP (file.php =>file)
 RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
  RewriteRule ^ /%1/ [L,R]
 #The rule bellow maps non PHP URLs to PHP
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.+?)/?$ /.php [L]

确保清除浏览器缓存或使用不同的浏览器来测试这些规则。