【问题标题】:.htaccess hide get param from url and modify url.htaccess hide 从 url 获取参数并修改 url
【发布时间】:2018-03-07 11:38:27
【问题描述】:

现在我有以下网址 http://example.com/products/?category=clothes 我的主要逻辑依赖于从 url 接收到的 get 参数。 有什么办法可以将结构更改为 http://example.com/products/clothes ,并且还有包含衣服的类别参数吗? 我尝试使用 htaccess - 这个我试过了,还是不行。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/([^/]+)$ products/$1/?category=$1 [L]

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您可以在.htaccess 中使用以下内容:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} category=(.+) [NC]
    RewriteRule ^(.*)$ http://example.com/products/%1? [R=301,NC,L]
    

    这将为您留下 URL:http://example.com/products/clothes

    我们在这里所做的是抓取变量category=,并使用%1 抓取与它相关的任何内容并将其放置在URL 的末尾。

    然后我们使用? 来阻止原始查询,例如,?category=clothes 不会出现在 URL 的末尾。

    确保在测试之前清除缓存。

    【讨论】:

    • 这不是在 GET 中保留类别参数。有什么方法可以通过 php $_GET 使其可用,但没有显示在 url 上?
    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多