htaccess的返回500错误时,需要将404 [英] htaccess returning 500 error when needs to be 404
问题描述
我怎样才能让这个我的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。
要进一步明确采取以下网址测试/蒸煮/ 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
这篇关于htaccess的返回500错误时,需要将404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!