本文介绍了的.htaccess - 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的.htaccess返回错误404,这并不符合下列URL模式标准的任何URL。我怎样才能做到这一点在.htaccess?

I want .htaccess to return error 404 for any url that does not meet below url pattern criteria. How can I do this in .htaccess?

在PHP脚本中使用这种code产生上述网址

Using this code in php script generates above url

$adscatcanonurl = "$script_url" . "/" . "{$vbasedir}$xcityid-$xcitynamealt/posts/$xcatid-$catname_inurl/". ("$xsubcatid"?"$xsubcatid-". RemoveBadURLChars($xsubcatname) :"");

样本链接

http://www.domain.com/17-Netherlands /职位/ 8-房地产/

假设脚本生成这样一个链接 http://www.domain.com/17-Netherlands/posts/8/

Let's say script generates a link like thishttp://www.domain.com/17-Netherlands/posts/8/

因为它不符合上面的URL模式匹配,此类URL应该返回404页没有响应。

As it's not matching with above url pattern, such urls should return 404 page not response.

推荐答案

您并不需要强制的.htaccess返回一个404,它只会做自己,如果没有匹配。所以,你可以使用规则,如:

You don't need to force .htaccess to return a 404, it will simply do so on its own if there is no match. So you can use a rule like:

RewriteEngine On
# Don't match real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(\d+)-([a-zA-Z-]+)/posts/(\d+)-([a-zA-Z-]+)$ your_php_script.php?xcityid=$1&xcitynamealt=$2&xcatid=$3&xcatname_inurl=$4 [L,QSA]

任何请求是不是一个实际的文件(CSS,JS等),不符合上述规则不匹配的任意的规则,因此应该返回一个404

Any request that isn't for an actual file (css, js, etc) and doesn't match the above rule won't match any rule, and should therefore return a 404.

请注意,在规则,我绘制了4组件,你会读出你的PHP作为 $ _ GET ['xcityid'] 等。

Note that in the rule, I mapped the 4 components to what would be read in your PHP as $_GET['xcityid'], etc.

这篇关于的.htaccess - 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!