htaccess 和資料庫連接相當成功
如果有人鍵入www.example.com/dsadsada它會顯示一個錯誤頁面
如果有人鍵入www.example.com/news/dsadsa它也會顯示一個錯誤頁面
但是當有人鍵入www.example.com/news/besthotelinthearea2019/dsadsadsadsa它時它不會顯示一個錯誤頁面,它仍然顯示該地區最好的酒店 2019 新聞但沒有 css,如何重定向它到 404 錯誤?非常感謝
這是目前我的代碼.htaccess
RewriteEngine on
ErrorDocument 404 /error.php
ErrorDocument 300 /error.php
RewriteRule ^index.html$ / [R=301,L]
RewriteRule ^(.*)/index.html$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteRule ^news/([0-9a-zA-Z_-] ) news.php?url=$1 [NC,L]
RewriteRule ^sectioncategory/([0-9a-zA-Z_-] ) sectioncategory.php?category=$1 [NC,L]
uj5u.com熱心網友回復:
RewriteRule ^news/([0-9a-zA-Z_-] ) news.php?url=$1 [NC,L]
因為此規則/正則運算式僅抓取 URL 的第二個斜杠部分并丟棄其余部分(可能導致重復內容問題并使您的網站容易被濫用)。例如。當您請求/news/besthotelinthearea2019/dsadsadsadsa時,它會將請求重寫為news.php?url=besthotelinthearea2019(該/dsadsadsadsa部分被有效地忽略)。就像你請求的一樣/news/besthotelinthearea2019。
在正則運算式中添加一個字串結尾錨 ( $),使其僅匹配/news/besthotelinthearea2019而不匹配/news/besthotelinthearea2019/<anything>。
例如:
RewriteRule ^news/([0-9a-zA-Z_-] )$ news.php?url=$1 [NC,L]
同樣的“問題”也適用于您的最后一條規則(即“sectioncategory”)。
注意:NC這里應該不需要該標志(除非news可以使用混合大小寫請求 - 不建議)并且可以簡化字符類。例如,上面等價于:
RewriteRule ^news/([\w-] )$ news.php?url=$1 [L]
速記字符類\w與[0-9a-zA-Z_].
在旁邊:
如果有人鍵入
www.example.com/news/dsadsa它也會顯示錯誤頁面
在這種情況下,“錯誤頁面”必須由您的腳本 ( news.php) 生成,而不是 Apache。
(而此答案第一部分中的 URL 現在將觸發 Apache ErrorDocument,因為它與您的規則不匹配。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510605.html
