【问题标题】:.htaccess redirect urls with spaces.htaccess 带有空格的重定向网址
【发布时间】:2019-01-04 08:50:10
【问题描述】:

我正在尝试通过我的 .htaccess 文件将所有无效 url 重定向到我的 index.php 文件。

不幸的是,我不断收到 Apache 错误。

我的 .htaccess 文件

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
RewriteRule ^([A-Za-z0-9\s]+)$ index.php?p=$1 [L] 

这个无效的 url 应该重定向到 index.php:

/vacatures/jobapplication/facility-manager%20qsdf 

但它会抛出 object not found 404 Apache 错误。

【问题讨论】:

    标签: php apache


    【解决方案1】:

    您拥有的允许空格的规则不允许使用连字符。您拥有的允许连字符的规则不允许使用空格。所以任何包含两者的东西都不会匹配。

    您的无效 URL facility-manager%20qsdf 包含两者。

    我的猜测是你的RewriteCond 应该适用于这两个规则,但现在不是这样,it will apply only to the first RewriteRule after it。您可以通过仅包含 1 个RewriteRule 并修改它以接受您想要的所有内容来解决所有这些问题:

    RewriteRule "^([A-Za-z0-9\-\_\/\s]+)$" index.php?p=$1 [L] 
    

    请注意,这需要您的角色类中的至少一个角色,换句话说,当没有路径时(“http://somewhere.com/”),它将与您的“家”位置不匹配。如果您还想匹配该位置,请将 + 更改为 *,以允许 0 个或多个字符匹配。

    【讨论】:

      【解决方案2】:

      您的重写规则与您指定的网址不匹配。您的 REQUEST_URI 是

      /vacatures/jobapplication/facility-manager%20qsdf 
      

      我怀疑在 RewriteRule 匹配之前没有完成 URL 解码,因此它试图匹配 %20,但 % 符号不包含在您的匹配中。我不确定你为什么要使用两个 RewriteRules - 为什么不这样做呢?

      RewriteEngine on
      RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
      RewriteCond %{REQUEST_URI} !^index.php(\?.*)?$
      RewriteRule ^(.*)$ index.php?p=$1 [L]
      

      【讨论】:

      • \s,在他的第二个 RewriteRule 中,将匹配任何空格。
      猜你喜欢
      • 2019-06-26
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      相关资源
      最近更新 更多