htaccess的网址用PHP重写 [英] .htaccess url rewriting with php
问题描述
目前,我对PHP和放大器运行整个网站; GET变量。
I currently have an entire website running on PHP & GET variables.
我的链接看起来像这样在present
My links look like this at present
http://www.example.co.uk/brochure.php?cat_path=24
和偶尔它在URL中的第二个变量...
And occasionally it has a second variable in the url...
其实,我想我的网址,如下所示: http://www.example.co.uk/Concrete_Fence_Posts
I actually would like my URLs to look like this: http://www.example.co.uk/Concrete_Fence_Posts
所以我的问题很简单,我怎么去重写URL没有打破我的GET变量。
So my questions is simply, how do I go about rewriting urls without breaking my GET variables.
感谢。
推荐答案
假设你的参数都命名为 cat_path
和的product_id
(如果存在的话),你可以做这样的事情:
Assuming your parameters are always named cat_path
and product_id
(if it exists) you can do something like this:
RewriteRule ^([^/]+)/([\d+])$ $1.php?cat_path=$2
RewriteRule ^([^/]+)/([\d+])/([\d+])$ $1.php?cat_path=$2&product_id=$3
您的网址然后将这些格式之一:
Your URLs would then be in one of these formats:
pagename/cat_path
pagename/cat_path/product_id
例如:
http://www.bentinckfencing.co.uk/brochure/24
http://www.bentinckfencing.co.uk/product/35/54
编辑:我看到你想使用的产品名称在URL中。在这种情况下,你的PHP脚本将需要能够取了个名字作为参数,并查找ID。您应该继续直接接受ID,以不破坏现有的链接。那么你的重写规则是这样的:
I see you want to use product names in the URL. In that case, your PHP scripts will need to be able to take a name as a parameter and look up the ID. You should continue to accept the ID directly so as to not break existing links. Then your rewrite rule would look like this:
RewriteRule ^([^.]+)$ brochure.php?name=$1
和 http://www.bentinckfencing.co.uk/Concrete_Fence_Posts 将重写< A HREF =http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts相对=nofollow> http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts 。
这篇关于htaccess的网址用PHP重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!