【问题标题】:htaccess - hide file extension and keep only value of a variablehtaccess - 隐藏文件扩展名并仅保留变量的值
【发布时间】:2021-06-29 16:20:42
【问题描述】:

原始网址 - example.com/art.php?a=lorem
希望显示为 - example.com/art/lorem
litespeed 服务器 - htaccess 已启用

RewriteEngine ON
RewriteRule ^art/(.*)$ /art.php?a=$1 [L]

没用
网址仍然是 - example.com/art.php?a=lorem
请帮忙

【问题讨论】:

    标签: .htaccess url-rewriting litespeed


    【解决方案1】:

    您只有一个重写规则来重写example.com/art/lorem,但您缺少一个重定向规则来将example.com/art.php?a=lorem 重定向到example.com/art/lorem

    你可以使用:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+art\.php\?a=([^\s&]+) [NC]
    RewriteRule ^ /art/%1? [R=301,L]
    
    RewriteRule ^art/([\w-]+)/?$ art.php?a=$1 [L,QSA]
    

    THE_REQUEST 变量代表 Apache 从您的浏览器收到的原始请求,并且在执行其他重写指令后它不会被覆盖。此变量的示例值为GET /index.php?id=123 HTTP/1.1

    参考资料:

    【讨论】:

    • 它有效,非常感谢,特别是您的解释。我对所有关于.htaccess 的事情完全感到困惑,并且找不到任何初学者参考。顺便说一句,看到这一行但类似的解决方案 - https://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
    • 我在答案中添加了一堆参考链接。
    猜你喜欢
    • 2021-07-11
    • 2014-03-29
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2013-02-03
    • 2017-10-06
    相关资源
    最近更新 更多