用get变量重写url

时间:2012-06-24 19:52:53

标签: .htaccess mod-rewrite url-rewriting

我想写<a href="product.php?id=5">作为产品/ 5.我不是在谈论用户输入doamin.com/product/5来获得结果,我正在寻找的是 - 当用户点击链接时,浏览器地址栏会将网址显示为domain.com/product/5。

有可能吗?非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

只需写下:

<a href="/product/5">LINK</a>

你还需要一个 .htaccess 文件来处理 mod_rewrite ,例如:

# turn mod_rewrite engine on 
RewriteEngine On

# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d

# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"

# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]

您将获得 $ _ GET ['_ route'] ,其值为 / product / 5 。其余的由您的PHP代码来解析该字符串。