简体   繁体   中英

htaccess specific queries rewrite

So I've got my htaccess to currently take anything after / and take it as a query.

So http://www.website.com/bacon is really 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)

However the query can only be of 3 different types. 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.

So essentially I only want those 3 specific queries to be accepted, everything else would need to redirect to a 404 page.

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:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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