简体   繁体   English

htaccess特定查询重写

[英]htaccess specific queries rewrite

So I've got my htaccess to currently take anything after / and take it as a query. 因此,我的htaccess当前可以在/之后接受任何内容,并将其作为查询。

So http://www.website.com/bacon is really http://www.website.com/index.php?type=bacon 因此, http://www.website.com/bacon实际上是http://www.website.com/index.php?type=bacon

The query is used to generate the type of content for the page. 该查询用于生成页面内容的类型。 (A div contains different information based on the query) (一个div根据查询包含不同的信息)

However the query can only be of 3 different types. 但是,查询只能是3种不同的类型。 SO I have a problem where is a user were to go to http://www.website.com/baconandcheese then the DIV would be empty and look awkward. 所以我有一个问题,用户要去http://www.website.com/baconandcheese,那么DIV将为空并且显得笨拙。

So essentially I only want those 3 specific queries to be accepted, everything else would need to redirect to a 404 page. 因此,基本上我只希望接受这3个特定查询,其他所有内容都需要重定向到404页面。

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

This code should do it 该代码应该做到这一点

#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]

But you could also simply send a 404 with PHP: 但是您也可以使用PHP发送404:

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

You can have your rule like this: 您可以使用如下规则:

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