【问题标题】:How to remove file extension (.php) using .htaccess如何使用 .htaccess 删除文件扩展名 (.php)
【发布时间】:2015-11-30 20:09:46
【问题描述】:

我想重写

www.example.com/story.php?id=123&title=abc&cat=sports

www.example.com/story/123?title=abc&cat=sports

我用过

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

它不工作。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting friendly-url


    【解决方案1】:
    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^GET\ /(story)\.php\?id=(\d+)&(\S+) [NC]
    RewriteRule ^story\.php$ /%1/%2?%3 [R=301,L]
    
    RewriteRule ^(story)/(\d+)/?$ /$1.php?id=$2 [L,QSA]
    

    上述规则应根据需要起作用。

    【讨论】:

    • 我试过了,但它显示“在此服务器上找不到请求的 URL /story.php。”我已经在“htaccess.madewithlove.be”中对此进行了测试,它显示错误“RewriteCond %{THE_REQUEST} ^GET\ /(story)\.php\?id=(\d+)&(\S+) [NC] 这个变量不是支持:%{THE_REQUEST}”。请帮忙。
    • @ghost 提供目录结构。 story.php 文件在哪里?您将 htaccess 文件放在哪里?
    • htaccess 和 story.php 都在根目录下。
    • @ghost 如果显示的错误是“在此服务器上找不到请求的 URL /story.php”,那么至少 story.php 不在根目录中.
    • 页面在那里。你能在htaccess.madewithlove.be 中查看htaccess 吗?它显示“RewriteCond %{THE_REQUEST} ^GET\ /(story)\.php\?id=(\d+)&(\S+) [NC] 此变量不受支持:%{THE_REQUEST}”
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多