不断变化的动态URL与的.htaccess [英] changing dynamic url with .htaccess
问题描述
我有以下URL结构:
www.mysite.com/temporary/articles.php?artid=1
我想和改变它
I would like to change it with
www.mysite.com/temporary/articles/article-title-here.
任何人都可以告诉我,我该怎么办呢?
Anyone can tell me how can I do that?
我也有这两个varialbes链接:
Also I have links with two varialbes in it:
www.mysite.com/temporary/products.php?catid=1&productid=1
而我试图使它:
Which I am trying to make it:
www.mysite.com/temporary/products.php/category-name/product-name
在此先感谢。
Thanks in advance.
卓然
推荐答案
要做到这一点,你会需要某种类型的脚本,可以映射文章/分类/产品名称为ID的。你是你自己的,只要写这个剧本,但它可以去的东西沿着这行:
To do this you're going to need some type of script that can map article/category/product names to IDs. You're on your own as far as writing this script, but it can go something along the lines of this:
- 如果有一个查询字符串变量
文章名
,然后取它的值,查找数据库(或任何你正在使用的存储),并请参阅文章ID是什么,然后加载/temporary/articles.php?artid=ID
,其中ID为给定标题的ID。 - 如果有一个查询字符串变量
产品名称
和/或类名
,查找数据库看类别和产品ID是什么,这些名字,然后加载/temporary/products.php?catid=1&productid=1
使用相应的标识。 - 否则,像做回一个404
- If there's a query-string variable
article-name
, then take its value, lookup the database (or whatever storage you are using) and see what the article ID is, then load/temporary/articles.php?artid=ID
, where ID is the ID of the given title. - If there's a query-string variable
product-name
and/orcategory-name
, lookup the database to see what the category and product IDs are for those names, then load/temporary/products.php?catid=1&productid=1
using the appropriate IDs. - otherwise, do something like return a 404
相关联的重写规则来做到这一点假设你写这个PHP脚本是 /my-controller.php
The associated rewrite rules to make this happen assuming that this php script you've written is at /my-controller.php
RewriteRule ^temporary/articles/(.+)$ /my-controller.php?article-name=$1 [L]
RewriteRule ^temporary/products.php/([^/]+)/(.+) /my-controller.php?category-name=$1&product-name=$2
因此,如果有人要求 http://www.mysite.com/temporary/articles/富-文章,它会被改写成 /my-controller.php?article-name=foo-article
键,当有人请求的 http://www.mysite.com/temporary/products.php/bars/foo-product 一>,它会被改写成 /my-controller.php?category-name=bars&product-name=foo-product
,这是给你写剧本做必要的ID查找,然后将它传递给无论是articles.php或products.php脚本。
So if someone requests http://www.mysite.com/temporary/articles/foo-article, it will be rewritten to /my-controller.php?article-name=foo-article
and when someone requests http://www.mysite.com/temporary/products.php/bars/foo-product, it will be rewritten to /my-controller.php?category-name=bars&product-name=foo-product
and it's up to the script you write to do the necessary ID lookup, then pass it to either the articles.php or products.php scripts.
这篇关于不断变化的动态URL与的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!