htaccess的404重定向页面不工作的子目录路径 [英] htaccess 404 page redirect not working for sub directory path

查看:281
本文介绍了htaccess的404重定向页面不工作的子目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的ErrorDocument 404 /404-error.php
 

这将重定向到404页面完全一样时, www.example.com/about 如果获得约不存在。但不会重定向到404页,当我访问 www.example.cpm /页/测试

其实,有一个名为页面和页面 www.example.cpm /页/测试不存在。它应该重定向到404页。

 选项+了FollowSymLinks -MultiViews
RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{THE_REQUEST} ^(GET | HEAD)。\ S([^] +)\ PHP [NC]

的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S([^。] +)\。PHP [NC]
重写规则^%1 [R,L,NC]

的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则^%{} REQUEST_FILENAME的.php [L]

的RewriteCond%{HTTP_HOST}!^ WWW \。(。*)$ [NC]
(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 301,L]

ErrorDocument的404 /404-error.php
ErrorDocument的500 /404-error.php
 

解决方案

您404处理器是找到,但问题是添加的.php 扩展你的重写规则。 的RewriteCond%{} REQUEST_FILENAME -f的.php 返回真 /页/测试哪怕只是 /page.php 找到。

让你的规则是这样的:

 的ErrorDocument 404 /404-error.php
ErrorDocument的500 /404-error.php

选项​​+的FollowSymLinks -MultiViews
RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{HTTP_HOST}!^ WWW \。 [NC]
(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 302,L]

的RewriteCond%{THE_REQUEST} ^(?:GET | HEAD)。\ S([^] +)\ PHP [NC]
重写规则^%1 [R,L,NC]

的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^([^] +?)/?$ $ 1.PHP [L]
 

ErrorDocument 404 /404-error.php

It will redirect to 404 page perfectly when access like www.example.com/about if about is not exists. But will not redirect to 404 page when I access www.example.cpm/page/test.

Actually there is page called 'page' and www.example.cpm/page/test not exists. It should redirect to 404 page.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s([^.]+)\.php [NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

ErrorDocument 404 /404-error.php
ErrorDocument 500 /404-error.php

解决方案

Your 404 handler is find but problem is your rewrite rule for adding .php extension. RewriteCond %{REQUEST_FILENAME}.php -f returns true for /page/test even if just /page.php is found.

Have your rules like this:

ErrorDocument 404 /404-error.php
ErrorDocument 500 /404-error.php

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=302,L]

RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.php [L]

这篇关于htaccess的404重定向页面不工作的子目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆