Htaccess特定查询重写


htaccess specific queries rewrite

所以我已经有了我的htaccess目前采取/之后的任何东西,并把它作为一个查询。

所以http://www.website.com/bacon实际上是http://www.website.com/index.php?type=bacon

查询用于生成页面的内容类型。(div根据查询包含不同的信息)

但是查询只能有3种不同的类型。所以我有一个问题,是一个用户去http://www.website.com/baconandcheese然后DIV将是空的,看起来很尴尬。

所以基本上我只希望这3个特定的查询被接受,其他的一切都需要重定向到404页面。

RewriteRule ^([^'.]+)$ index.php?type=$1 [L]

你可以这样设置你的规则:

RewriteEngine On
RewriteRule ^(bacon|cheese|ham)/?$ index.php?type=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . - [L,R=404]

这段代码应该这样做

#Only your three types
RewriteRule ^bacon$ http://www.website.com/index.php?type=bacon [nc]
RewriteRule ^type2$ http://www.website.com/index.php?type=type2 [nc]
RewriteRule ^type3$ http://www.website.com/index.php?type=type3 [nc]
#Pass files and folders
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#Throw 404
RewriteRule ^([^'.]+)$ - [L,R=404]

但是你也可以简单地用PHP发送404:

header('HTTP/1.1 404 Not Found');
exit();