如果url不包含某些字符串,则htaccess重定向

问题描述:

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

I want to redirect all incoming requests to another url if it doesn't contain # and admin. I need it for angular.js, but I have /admin with php

例如:

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

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

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

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

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

还请记住,Web服务器不会在#之后添加URL,因为该问题仅在客户端解决.

Also remember that web server doesn't URL part after # as that is resolved only on client side.

  • RewriteCond%{REQUEST_FILENAME}!-f 跳过所有文件的此规则.
  • 使用会取消 RewriteRule 模式中的整个匹配
  • (((admin | login)(/.*)?)?是否与/admin//login/的任何内容匹配?空缺(着陆页)
  • 如果否定为true,则此规则会将其重定向到/#%{REQUEST_URI} ,其中%{REQUEST_URI} 代表原始URI.
  • RewriteCond %{REQUEST_FILENAME} !-f skips this rule for all files.
  • Using ! negates whole match in RewriteRule pattern
  • ((admin|login)(/.*)?)? matches anything that is /admin/ or /login/ OR emptry (landing page)
  • If negation is true then this rule redirects it to /#%{REQUEST_URI} where %{REQUEST_URI} represents original URI.