url-rewrite&如何使用htaccess进行设置

时间:2013-04-27 16:01:01

标签: .htaccess url-rewriting

我刚刚加载并激活了重写引擎,但无法理解 如何设计一个好的.htaccess文件。 我想转换这样的链接:

http://localhost/index.php?u=username --> http://localhost/username/
http://localhost/index.php?id=1&v=2 --> http://locahost/1x2

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

重写URL以进行更改:

  

ht * p:// localhost / username / - 以/结尾是可选的
    到> HT * P://localhost/index.php U =用户名

     

ht * p:// localhost / 1x2 - 不区分大小写(匹配4X4)
    到> HT * P://localhost/index.php ID = 1&安培; V = 2

.htaccess 内容:

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteCond %{REQUEST_URI} ^/([^/\d]+)/?$ [NC]
RewriteRule (.*) index.php?u=%1 [L]

RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{REQUEST_URI} ^/(\d+)x(\d+)/?$ [NC]
RewriteRule (.*) index.php?id=%1&v=%2 [L]