嵌套目录的 Htaccess 404 重定向自定义规则
Htaccess 404 redirect custom rule for nested directories
我有一个带有自定义 .htaccess 文件的站点,它可以处理一些事情:
1) 它将没有“.php”扩展名的 urls 视为末尾有“.php”。
2) 它重定向 http:// 和 http://www. urls to https://www。
这是 .htaccess 文件:
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ .php [L]
ErrorDocument 404 /404.php
一切都按预期工作,但我在网站管理员工具中发现了一些奇怪的 uls,这些 uls 导致 500 错误而不是 404:
当您访问不存在的根级别 url 时,例如 https://www.example.com/skjdfhkj 它会按预期重定向到 404。
但是,当您访问一些嵌套的 url 例如 https://www.example.com/some-page/skjdfhkj 时,其中 some-page
与现有 php 文件的名称匹配,然后它会给出一个 500 服务器错误。当 some-page
不匹配任何现有的 php 文件时,它会按预期 returns 404。
该站点中的所有 ulr 都是根级别的,任何地方都没有嵌套的文件夹结构。我已经检查了代码,没有指向嵌套 url 的链接,所以不确定他们是如何到达网站管理员工具的,很可能在网站上的某个位置存在一些错误链接,但这些链接已不复存在。
我的问题是,当有人访问 https://www.example.com/some-page/skjdfhkj 等不存在的嵌套 url 时,如何正确更新我的 htaccess 以 return 404?
All the ulrs in this site are root level and there are no nested folder structure anywhere.
您可以使用此代码块替换所有 .htaccess 代码:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/?$ .php [L]
我有一个带有自定义 .htaccess 文件的站点,它可以处理一些事情:
1) 它将没有“.php”扩展名的 urls 视为末尾有“.php”。
2) 它重定向 http:// 和 http://www. urls to https://www。
这是 .htaccess 文件:
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ .php [L]
ErrorDocument 404 /404.php
一切都按预期工作,但我在网站管理员工具中发现了一些奇怪的 uls,这些 uls 导致 500 错误而不是 404:
当您访问不存在的根级别 url 时,例如 https://www.example.com/skjdfhkj 它会按预期重定向到 404。
但是,当您访问一些嵌套的 url 例如 https://www.example.com/some-page/skjdfhkj 时,其中 some-page
与现有 php 文件的名称匹配,然后它会给出一个 500 服务器错误。当 some-page
不匹配任何现有的 php 文件时,它会按预期 returns 404。
该站点中的所有 ulr 都是根级别的,任何地方都没有嵌套的文件夹结构。我已经检查了代码,没有指向嵌套 url 的链接,所以不确定他们是如何到达网站管理员工具的,很可能在网站上的某个位置存在一些错误链接,但这些链接已不复存在。
我的问题是,当有人访问 https://www.example.com/some-page/skjdfhkj 等不存在的嵌套 url 时,如何正确更新我的 htaccess 以 return 404?
All the ulrs in this site are root level and there are no nested folder structure anywhere.
您可以使用此代码块替换所有 .htaccess 代码:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)/?$ .php [L]