【问题标题】:Rewriting a URL with GET Parameters to .pdf将带有 GET 参数的 URL 重写为 .pdf
【发布时间】:2021-10-01 19:53:24
【问题描述】:

假设我在 WordPress 中有这样的动态 URL:

https://example.com/category/post-title/?format=pdf

https://example.com/category2/post-title2/?format=pdf

我希望它们被重写为:

https://example.com/category/post-title.pdf

https://example.com/category2/post-title2.pdf

我如何使用 htaccess 做到这一点?

我尝试了类似的方法,但它不起作用:

#Options +FollowSymLinks
#RewriteEngine on
#RewriteRule ^.(.*) /?format=$1

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite


    【解决方案1】:

    你可以这样做:

    RewriteEngine On
    
    RewriteRule ^([^/]+)/(.+?)\.pdf$ /$1/$2?format=pdf [L]
    

    这将在内部将 /folder/foobar.pdf 的请求映射到 /folder/foobar?format=pdf 。请记住将此代码放在 htaccess 顶部或 WordPress 重写规则之前。

    【讨论】:

    • 谢谢!我无法让它工作。 /文件夹是帖子的类别,它随类别名称而变化。我试过这个并输入类别名称:RewriteRule ^([^/]+)/(.+?)\.pdf$ /category-name/$1/?format=pdf [L]
    • @Uklove 我已经编辑了答案中的代码。有一个错字也已修复。请立即尝试更新版本。
    • 我什么都试过了,直到我写了完整的路径 RewriteRule ^([^/]+)/(.+?)\.pdf$ https://example.com/$1/$2/?format=pdf [L] 才奏效。也许这是特定于服务器的,感谢您为我指明正确的方向。
    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多