Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP-MySQL.htaccess重写URL&;收到_Php_.htaccess_Mod Rewrite_Url Rewriting_Seo - Fatal编程技术网

用PHP-MySQL.htaccess重写URL&;收到

用PHP-MySQL.htaccess重写URL&;收到,php,.htaccess,mod-rewrite,url-rewriting,seo,Php,.htaccess,Mod Rewrite,Url Rewriting,Seo,我有一个包含5000个项目的MySQL数据库。该表的结构如下所示 id 产品名称 内容 产品url 您可以通过访问www.site.com/products/index.php?id=123访问特定的产品页面。php文件使用php获取ID以搜索MySQL数据库并返回适当的产品名称和内容 我怎样才能使它成为这样而不是www.site.com/products/index.php?id=123,URL被重写或重定向到www.site.com/products/product-URL?如果我使用.ht

我有一个包含5000个项目的MySQL数据库。该表的结构如下所示

id
产品名称
内容
产品url

您可以通过访问www.site.com/products/index.php?id=123访问特定的产品页面。php文件使用php获取ID以搜索MySQL数据库并返回适当的产品名称和内容

我怎样才能使它成为这样而不是www.site.com/products/index.php?id=123,URL被重写或重定向到www.site.com/products/product-URL?如果我使用.htaccess执行此操作,index.php文件是否仍然能够知道如果该ID不在url中,将使用什么ID来查找产品url?或者有没有办法用PHP和MySQL映射URL


(说到这件事,我是个不速之客,非常感谢您的帮助。谢谢。)

您可以使用.htaccess和重写规则。下面将使用
$\u GET['product-URL']
将SEO URL重定向到index.php。您需要修改代码以查找产品
id
或直接使用
产品url
。在产品表中,两者都应该是唯一的

RewriteEngine On
RewriteBase /

# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]

注意:我已将
产品url
设置为只允许字母、数字、破折号和下划线。我建议这样的限制。此外,尾部斜杠是可选的。此外,这不会重新结束查询字符串。如果需要,您可以将
QSA
标志添加到重写规则。

您可以使用.htaccess和重写规则。下面将使用
$\u GET['product-URL']
将SEO URL重定向到index.php。您需要修改代码以查找产品
id
或直接使用
产品url
。在产品表中,两者都应该是唯一的

RewriteEngine On
RewriteBase /

# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]

注意:我已将
产品url
设置为只允许字母、数字、破折号和下划线。我建议这样的限制。此外,尾部斜杠是可选的。此外,这不会重新结束查询字符串。如果需要,您可以将
QSA
标志添加到重写规则中。

我在产品url中添加了关于句点的注释,但我刚刚删除了.php扩展名,现在它就像一个符咒!非常感谢。我看不到你的评论。您想在产品url中使用句点吗?我建议不要这样做。我在产品url中添加了一条关于句点的评论,但我刚刚删除了.php扩展名,现在它就像一个符咒!非常感谢。我看不到你的评论。您想在产品url中使用句点吗?我建议不要那样做。