【问题标题】:.htaccess: clean URL with parameter "images" doesn't work.htaccess:带有参数“images”的干净 URL 不起作用
【发布时间】:2014-12-13 08:28:25
【问题描述】:

我刚刚开始尝试弄清楚如何使用 .htaccess 制作干净的 URL,但似乎我马上就遇到了困难……我有以下简单的 .htaccess:

RewriteEngine on

RewriteRule ^([a-zA-Z0-9-]+)$ index.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?id=$1 [L]

它大部分都按预期工作:mysite.com/somepage 转到 mysite.com/index.asp?id=somepage 等。

除非 id 是“图像”。当我尝试 mysite.com/images 时,它会显示:mysite.com/images/?id=images(尽管页面仍然可以正常加载)

顺便说一下,mysite.com/images/(带有斜线)可以正常工作。过去几天我一直在寻找解决方案,这是我能得到的最好的解决方案。

【问题讨论】:

    标签: .htaccess url mod-rewrite url-parameters


    【解决方案1】:

    由于images 是一个目录,Apache 的mod_dir 模块会在您的重写规则之后添加一个斜杠。您可以关闭此功能并自己添加尾部斜杠:

    DirectorySlash Off
    RewriteEngine on
    
    # add a trailing slash
    RewriteCond %{DOCUMENT_ROOT}/$1 -d
    RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?id=$1 [L,QSA]
    

    【讨论】:

    • 是的,做到了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多