簡體   English   中英

.htaccess用於干凈的URL

[英].htaccess for clean url

我列出了州和一個php文件,其中列出了該州的所有城市。 通過將URL參數設為/common.php/?state=Texas並列出所有城市。 單擊城市后,其網址為/common.php/?city=Alanreed&state_code=TX並顯示城市信息。

我想使用.htaccess格式化網址,例如對於州,它應該使用state_name.html eg. Texas.html eg. Texas.html以及城市名稱Texas/sugar_land.htmltx/sugar_land.html如何使用php和.htaccess做到這一點?

RewriteEngine On
RewriteRule ^(.*)/(.*).html$ common.php/?state=$1&city=$2 [L]
RewriteCond %{REQUEST_URI} !^(.*/)?index\.html$ [NC]
RewriteRule ^(.*).html$ common.php/?state=$1 [L]

\\w{2}表示2個字字符,因此將其重寫為狀態碼,第二條規則是重寫完整的狀態名稱,第三條規則是重寫狀態。

嘗試這樣的事情:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^([a-z]{2})\.[hH][tT][mM][lL]?$ common\.php\?state_code=$1
    RewriteRule ^([a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state=$1

    RewriteRule ^([a-z]{2})/([_a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state_code=$1&city=$2
    RewriteRule ^([a-zA-Z]+)/([_a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state=$1&city=$2
</IfModule>

也應該適用於tx.htm。這假設您的apache啟用了mod_rewrite。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM