簡體   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