htaccess的返回500错误时,需要将404错误、htaccess

由网友(梦见你哭却不能拥抱)分享简介:我怎样才能让这个我的htaccess的规则返回404时,无效的网址输入到地址栏。这是造成此两个规则重写规则^提交/([AZ] +)/([0-9] +)$ /提交1 C = $&放大器; I = $ 2 [L]重写规则^测试/([AZ] +)/([0-9] +)/([0-9] +)$ /测试1 C = $&放大器; I...

我怎样才能让这个我的htaccess的规则返回404时,无效的网址输入到地址栏。

这是造成此两个规则

 重写规则^提交/([AZ] +)/([0-9] +)$ /提交1 C = $&放大器; I = $ 2 [L]

重写规则^测试/([AZ] +)/([0-9] +)/([0-9] +)$ /测试1 C = $&放大器; I = $ 2及Q = $ 3 [L]
 

这两个规则做工精细进入,但说,例如在过去的一个参数是不是一个数字一个正确的URL时,它会返回500错误。

我怎样才能让这个如果这些规则不匹配的话,它会返回一个404。

服务器常见页面访问返回错误信息 404 500 400 表示什么意思

要进一步明确采取以下网址测试/蒸煮/ 9/1 是有效的。那么,提交/蒸煮/ 9

它会返回500的URL不匹配的改写,如这些,测试/蒸煮/ 9/1/1的提交/ 0/0 / 0`,我想它返回一个404,如果上述规则是不会匹配。

FULL .htaccess文件

 #开启重写引擎
RewriteEngine叙述上

#内部重写提交参数
重写规则^提交/([AZ] +)/([0-9] +)$ /提交1 C = $&放大器; I = $ 2 [L]

#内部重写测试参数
重写规则^测试/([AZ] +)/([0-9] +)/([0-9] +)$ /测试1 C = $&放大器; I = $ 2及Q = $ 3 [L]

#内部重写的.php
的RewriteCond%{REQUEST_FILENAME}  PHP -f
重写规则^ /?(。*)$ /$1.php [L]

#外部重定向的.php
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9}  /([^ ] +)。PHP
重写规则^ /?(。*)。PHP / $ 1 [L,R = 301]

#添加默认字符集的文件
AddDefaultCharset UTF-8

#G-ZIP紧缩COM pression
< FilesMatch(JS | CSS |全文| HTM | PHP | XML)$。>
     SetOutputFilter DEFLATE
< / FilesMatch>
 

解决方案

这是发生因为Apache正在读取的URI路径信息的一部分,而测试部分提交是匹配的 -f 检查eventhough路径的其余部分没有。试着改变 -f 检查:

 的RewriteCond%{DOCUMENT_ROOT}%{REQUEST_URI}  PHP -f
 

来代替:

 的RewriteCond%{REQUEST_FILENAME}  PHP -f
 

How can I make it so that my htaccess rules return a 404 when invalid urls are entered into the address bar.

The two rules that are causing this

RewriteRule ^submit/([a-z]+)/([0-9]+)$ /submit?c=$1&i=$2 [L]

RewriteRule ^test/([a-z]+)/([0-9]+)/([0-9]+)$ /test?c=$1&i=$2&q=$3 [L]

Both the rules work fine when a correct url is entered but say for example one of the last parameters is not a number, it returns a 500 error.

How can I make it so that if these rules are not matched then it'll return a 404.

To further clarify take the following url test/cooking/9/1 is valid. So is submit/cooking/9.

It returns 500 with urls that dont match the rewrite such as these, 'test/cooking/9/1/1,submit/0/0/0`, I would like it to return a 404 if the rules outlined above are NOT matched.

FULL .htaccess FILE

# Turn on rewriting engine
RewriteEngine On

# Internally rewrite submit parameters
RewriteRule ^submit/([a-z]+)/([0-9]+)$ /submit?c=$1&i=$2 [L]

# Internally rewrite test parameters
RewriteRule ^test/([a-z]+)/([0-9]+)/([0-9]+)$ /test?c=$1&i=$2&q=$3 [L]

# Internally rewrite .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

# Externally redirect .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^ ]+).php
RewriteRule ^/?(.*).php$ /$1 [L,R=301]

# Add default charset for files
AddDefaultCharset UTF-8

# G-zip deflate compression
<FilesMatch ".(js|css|html|htm|php|xml)$">
     SetOutputFilter DEFLATE
</FilesMatch>

解决方案

This is happening because apache is reading the URI as part of the PATH INFO, and parts of test or submit is matching the -f check eventhough the rest of the path doesn't. Try changing the -f check to:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f

instead of:

RewriteCond %{REQUEST_FILENAME}.php -f

阅读全文

相关推荐

最新文章