简体   繁体   English

.htaccess用于干净的URL

[英].htaccess for clean url

I've list of states and a php file which will list all the city of the state. 我列出了州和一个php文件,其中列出了该州的所有城市。 By taking the URL parameter as /common.php/?state=Texas and list all the cities. 通过将URL参数设为/common.php/?state=Texas并列出所有城市。 After clicking on the city it takes the url as /common.php/?city=Alanreed&state_code=TX and displays the city information. 单击城市后,其网址为/common.php/?city=Alanreed&state_code=TX并显示城市信息。

I want to format the url using .htaccess like for states it should take state_name.html eg. Texas.html 我想使用.htaccess格式化网址,例如对于州,它应该使用state_name.html eg. Texas.html eg. Texas.html and for city name either Texas/sugar_land.html or tx/sugar_land.html How can I achieve this using php and .htaccess? 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} means 2 word characters, so it is rewritten as a state code, the second rule is to rewrite the full state names and the third is to rewrite states. \\w{2}表示2个字字符,因此将其重写为状态码,第二条规则是重写完整的状态名称,第三条规则是重写状态。

Try something like this: 尝试这样的事情:

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

Should also work for tx.htm This assumes that your apache has mod_rewrite enabled. 也应该适用于tx.htm。这假设您的apache启用了mod_rewrite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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