【问题标题】:Error getting value of a query string获取查询字符串的值时出错
【发布时间】:2017-03-02 18:21:22
【问题描述】:

我使用这个 .htaccess 文件删除了我的项目文件夹中所有扩展名为 .php 的文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]

现在,例如,home/product.php 变为 home/product。 但是我有一个问题,我有一个带有这样的查询字符串的网址 - “home/product_details.php?id=$id”。我只能访问“home/product_details”。我希望它像“home/product_details/$id”。

请问我该怎么做以及如何获得 $id 的值?

我通常会这样做:

$id = $_GET['id'];

我想我需要在 .htaccess 中进行一些调整,但我不知道。

谢谢。

【问题讨论】:

  • 你使用任何 php 框架吗?

标签: php .htaccess


【解决方案1】:

您可以使用以下规则:

RewriteEngine on
#remove .php exten
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*?)/?$ $1.php [L]
#rewrite /file/foobar to /file.php?id=foobar
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ /$1.php?id=$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2023-03-28
    • 2011-09-12
    相关资源
    最近更新 更多