【问题标题】:Htaccess not working when hyphen (-) is used in url在 url 中使用连字符 (-) 时 Htaccess 不起作用
【发布时间】:2013-07-05 09:42:21
【问题描述】:

我的 htaccess 中有以下代码

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .?- [S=2]
RewriteRule ^abc/(.*)/(.*)$ index.php?aa=$1&bb=$2 [NE,L,QSA]
RewriteRule ^abc/(.*)$ index.php?aa=$1 [NE,L,QSA]

当我通过以下网址时

http://example.com/abc/some-example?id=123

按照我的说法,我应该在 PHP 中使用 $aa = some-example 和 $id = 123

但我收到以下错误

Not Found

The requested URL /abc/[S=2] was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

当我传递如下网址时,上面的 .htaccess 文件可以正常工作

http://example.com/abc/someexample?id=123

【问题讨论】:

    标签: php .htaccess url mod-rewrite http-status-code-404


    【解决方案1】:

    我认为您在这一行的破折号前缺少一个空格:

    RewriteRule .?- [S=2]
    

    应该是:

    RewriteRule .? - [S=2]
    

    编辑:为什么你还有这条规则?尝试删除它。

    据我所知,如果请求与特定文件不匹配,您就是在告诉 apache 跳过接下来的两条规则,这会导致 404 而不是重写。

    您可能还想在友好的 404 页面末尾添加一条规则,以防请求的 url 与您的重写正则表达式不匹配。

    【讨论】:

    • 感谢瑞恩的帮助。如果我给空间,那么我给的每条路径都会向我显示下面的错误Not Found The requested URL /a/someexample/ was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
    • 在网上搜索后,我发现您的建议是正确的做法。但是,我不知道为什么我没有得到我正在寻找的输出。 :(
    【解决方案2】:

    感谢瑞恩,

    改变我的

    RewriteRule .?- [s=2]

    到

    RewriteRule ./ - [s=2]
    

    帮我找到了我想要的东西

    再次感谢瑞恩。

    【讨论】:

    • 你确定你甚至需要这个规则吗?如果删除线会发生什么?
    • 确实如此。我不想要我的下一行.. :(.. RewriteRule ^abc/(.*)$ index.php?aa=$1 [NE,L,QSA]..
    • 很高兴它成功了。如果我的答案是正确的,请标记为答案。
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多