【问题标题】:Apply rewrite rule if url not contains certain path如果 url 不包含特定路径,则应用重写规则
【发布时间】:2014-10-10 17:01:40
【问题描述】:

我已经为使用 html5 路由的 AngularJs 应用程序编写了这个重写规则。

重写的url是这样的:

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|html|woff)$
 RewriteRule ^ index.html  [L]

我希望不重写包含路径 /old/ 和路径 /images/ 的所有 URL!

有人可以帮助我,我尝试像这样添加重写条件:

 RewriteCond %{REQUEST_URI} !^/old/

但它不起作用!

问候, 洛伦佐

更新

如果您使用带有路由的 AngularJS,请在 <a> 元素上使用 target="_self",该元素在同一域中具有链接但不在角度路由下!

【问题讨论】:

    标签: angularjs apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您的第三个RewriteCond 没用,因为您已经在第一个条件下检查现有文件。

    另外,如果您在子目录中(如/old/ 或其他内容,则应在index.html 中添加RewriteBase 或前导斜杠。否则它将在当前目录中查找(可能是404 错误)。

    你可以用这个替换你的代码

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(old|images)/ [NC]
    RewriteRule . /index.html  [L]
    

    【讨论】:

      猜你喜欢
      • 2015-08-17
      • 2018-04-05
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      相关资源
      最近更新 更多