RewriteEngine ON
# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&] ) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteRule ^([^/.] )/?$ index.php?c=$1 [L,QSA]
以上作業正常,現在我想在內部example.com/contact被改寫成example.com/contact.php 我嘗試過的地方:
# contact -> contact.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^contact$ /contact.php? [L]
問題 -index.php如果我洗掉第一個代碼塊(關于 index.php),這會將我重定向到- 第二部分作業正常 - 沒有重定向到 index.php,第二個似乎有問題RewriteCond。
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試遵循 htaccess 規則檔案。在測驗您的 URL 之前,請確保清除瀏覽器快取。
RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact)/?$ $1.php [QSA,NC,L]
# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?\?c=(\S )\s [NC]
RewriteRule ^ %1? [R=301,L,NE]
##For internal rewrite rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c=$1 [L,QSA]
或者,如果您必須在內部將about 重寫為about.php 或contact 為contact.php,那么您可以使用以下規則。
RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact|about)/?$ $1.php [QSA,NC,L]
# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?\?c=(\S )\s [NC]
RewriteRule ^ %1? [R=301,L,NE]
##For internal rewrite rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c=$1 [L,QSA]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389459.html
標籤:阿帕奇 .htaccess 改写 url重写 友好网址
