【问题标题】:Windows 10 Xampp Apache - htaccess rewriterule not workingWindows 10 Xampp Apache - htaccess rewriterule 不起作用
【发布时间】:2022-01-23 00:32:33
【问题描述】:

我在Windows 10 上使用apache XAMPP 并试图让htaccess 工作。到目前为止我有这个......

RewriteEngine On
RewriteRule ^noexist/?$ /folder/

我想找个人去

www.mysite.com/noexist/test.php -> www.mysite.com/folder/test.php

但我仍然希望 URL 为 www.mysite.com/noexist/test.php

我刚刚收到 404,我哪里出错了?我已经通过输入无效代码确认正在加载 htaccess 文件,它会引发错误,所以我确定该文件正在被使用。

【问题讨论】:

    标签: apache .htaccess mod-rewrite xampp


    【解决方案1】:
    RewriteRule ^noexist/?$ /folder/
    

    正则表达式 ^noexist/?$ 仅匹配 noexistnoexist/,因此此规则将忽略 /noexist/test.php。它也只重写为/folder/

    换句话说,这只会将/noexist(或/noexist/)重写为/folder/

    要将/noexist/<something> 重写为/folder/<something>,您需要捕获<something> 部分并将其传递给目标URL(即substitution 字符串)。例如:

    RewriteRule ^noexist/(.*) /folder/$1 [L]
    

    substitution 字符串中的$1 反向引用包含由RewriteRulepattern 中带括号的子模式(即(.*))捕获的 URL 路径。

    不要忘记L (last) 标志。 (如果文件后面有其他指令,这很重要。)

    请注意,此重写是无条件的,无论/folder/<something> 是否存在。如果你想在重写之前检查/folder/<something> 是否存在,那么添加一个额外的条件。例如:

    RewriteCond %{DOCUMENT_ROOT}/folder/$1 -f
    RewriteRule ^noexist/(.*) /folder/$1 [L]
    

    这假设您的 .htaccess 文件位于文档根目录中。

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2012-12-30
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2012-12-10
      • 1970-01-01
      相关资源
      最近更新 更多