htaccess的网址用PHP重写 [英] .htaccess url rewriting with php

查看:235
本文介绍了htaccess的网址用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 =htt​​p://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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆