【问题标题】:Remove "?" from the query string消除 ”?”从查询字符串
【发布时间】:2018-03-18 21:48:51
【问题描述】:

我决定放弃 Wordpress,转而使用 Pico CMS

现在我正在努力正确完成 URL 重写。

我已经设法从例如重写查询字符串。 https://www.example.org?pagehttps://www.example.org/page 使用 RewriteRule ^(.*) ?$1 [L],但现在如果有 查询字符串,我也希望它重写。

例子:

如果请求https://www.example.org?page,它可以正常加载,但我希望将其重写为https://www.example.org/page - 至少在地址栏中。

我尝试了几种变体,但都无济于事。我很讨厌正则表达式...

RewriteCond %{QUERY_STRING} !^$ RewriteRule ^\\?/(.*)$ $1

没有

RewriteRule (^\?$) https://tanghus.net/$1 [NC,R=301,L] RewriteRule ^.*$ https://tanghus.net/? [NC,R=301,L]

没有骰子

RewriteCond %{QUERY_STRING} . RewriteRule ^$ %{QUERY_STRING} [R,L]

RewriteCond %{THE_REQUEST} \?\sHTTP [NC] RewriteRule ^ %{REQUEST_URI} [L,R]

放弃:(

【问题讨论】:

    标签: mod-rewrite query-string


    【解决方案1】:

    你可以使用这条规则

    RewriteEngine on
    
    # externally  redirect from /?page to /page
    RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
    RewriteRule ^/?$ /%1? [L,R]
    # internally map /page to /?page
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/?(.+)$ /?$1 [L]
    

    【讨论】:

    • 这绝对成功了!非常感谢。我真的应该在 20 多年前更加关注 RegExps ;)
    猜你喜欢
    • 2015-11-28
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多