首页 > 解决方案 > HTACCESS URL 重写

问题描述

请问如何使用 .htaccess 检索以下 URL 的 abc123 部分?

https://a.com/product/abc123

我需要使用 abc123 部分来创建查询。通常,我知道这可以通过以下方式实现:

https://a.com/product/?key=abc123

并像这样使用 $_GET 获取 abc123 部分:

$product_key = $_GET['key'];

但我更喜欢 htaccess 方法,我需要帮助。

标签: php.htaccess

解决方案


在您的 .htaccess 文件中放置并使用此规则

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?product/(.*?)/?$ /index.php?key=$1 [L]

推荐阅读