【问题标题】:htaccess redirect if url doesn't contain some string如果 url 不包含某些字符串,则 htaccess 重定向
【发布时间】:2014-12-28 02:01:30
【问题描述】:

如果它不包含 # 和 admin,我想将所有传入请求重定向到另一个 url。我需要它用于 angular.js,但我有 /admin 和 php

例如:

http://example.com/link-to-article -> http://example.com/#/link-to-article

http://example.com/admin/* 不会重定向

【问题讨论】:

    标签: php angularjs .htaccess


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^((admin|login)(/.*)?)?$ /#%{REQUEST_URI} [L,NC,NE,R=302]
    

    还请记住,Web 服务器在 # 之后没有 URL 部分,因为这仅在客户端解决。

    • RewriteCond %{REQUEST_FILENAME} !-f 为所有文件跳过此规则。
    • 使用! 否定RewriteRule 模式中的整个匹配
    • ((admin|login)(/.*)?)? 匹配任何 /admin//login/ 或空(登陆页面)
    • 如果否定为真,则此规则将其重定向到 /#%{REQUEST_URI},其中 %{REQUEST_URI} 表示原始 URI。

    参考资料:

    【讨论】:

    • "此网页有重定向循环" :/ 如何检查 URL 是否不包含 # ?
    • 不起作用。无法加载 /js 和其他依赖项。我可以在一行中检查多个网址吗? admin、js、css、字体、img、数据和视图?
    • 我可以添加另一个不重定向的网址吗? /admin 和 /login 例如
    • 修改了规则,并添加了参考链接的解释。
    • @anubhava:我只想在请求来自 android 并且 url 没有字符串 'api' 或 'rest' 时重定向,尝试过 # Android devices RewriteCond %{HTTP_USER_AGENT} Android RewriteCond %{REQUEST_URI } !/pwa RewriteRule ^(.*)$ /pwa/$1 [NC,L] #RewriteRule !^((rest|api)(.*))$ /pwa/$1 [NC,L] 但不工作,请帮助stackoverflow.com/questions/55290651/…
    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2014-02-11
    相关资源
    最近更新 更多