[英]URL rewriting with PHP MySQL .htaccess & GET
I have a MySQL database with 5000 items. 我有一个包含5000个项目的MySQL数据库。 The table is structured as
该表的结构为
id ID
product-name 产品名称
content 内容
product-url 产品链接
You can access a specific product page by going to www.site.com/products/index.php?id=123. 您可以访问www.site.com/products/index.php?id=123访问特定产品页面。 The index.php file uses PHP to GET the ID to search the MySQL database and return the appropriate product name and content.
index.php文件使用PHP来获取ID以搜索MySQL数据库并返回相应的产品名称和内容。
How can I make it so instead of www.site.com/products/index.php?id=123, the URL is rewritten or redirected to www.site.com/products/product-url? 我怎样才能这样做而不是www.site.com/products/index.php?id=123,URL被重写或重定向到www.site.com/products/product-url? If I do it with .htaccess, will the index.php file still be able to know what ID to use to look up the product-url if the ID is not in the URL?
如果我使用.htaccess执行此操作,如果ID不在URL中,index.php文件是否仍然能够知道用于查找product-url的ID? Or is there some way to map URLs with PHP and MySQL?
或者有没有办法用PHP和MySQL映射URL?
(I'm a noob when it comes to this stuff, so any help will be much appreciated. Thanks.) (当谈到这些东西时,我是一个菜鸟,所以任何帮助都会非常感激。谢谢。)
You can use .htaccess and a RewriteRule. 您可以使用.htaccess和RewriteRule。 The following will redirect an SEO URL to index.php with
$_GET['product-url']
. 以下将使用
$_GET['product-url']
将SEO URL重定向到index.php。 You will need to modify your code to either lookup the product id
or use the product-url
directly. 您需要修改代码以查找产品
id
或直接使用product-url
。 Both should be unique in your products table. 两者在您的产品表中应该是唯一的。
RewriteEngine On
RewriteBase /
# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]
Note: I've made the product_url
to only allow letters, numbers, dashes and underscores. 注意:我已将
product_url
设为仅允许字母,数字,短划线和下划线。 I suggest such a restriction. 我建议这样的限制。 In addition, the trailing slash is optional.
此外,尾部斜杠是可选的。 Futhermore, this will not reappend a query string.
此外,这不会重新使用查询字符串。 If you need that you can add the
QSA
flag to the RewriteRule. 如果需要,可以将
QSA
标志添加到RewriteRule。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.