繁体   English   中英

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]

该代码应该做到这一点

#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();

您可以使用如下规则:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM