4

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

For example:

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

http://example.com/admin/* will not redirect

Levan Lotuashvili
  • 841
  • 10
  • 13

1 Answers1

5

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]

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

  • 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.

References:

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • "This webpage has a redirect loop" :/ How to check if URL doesn't contain # ? – Levan Lotuashvili Dec 27 '14 at 17:42
  • 1
    Doesn't work. Can't load /js and other dependencies. Can I check multiple urls in one line? admin, js, css, fonts, img, data and views? – Levan Lotuashvili Dec 27 '14 at 17:47
  • And can I add another url to not redirect? /admin and /login for example – Levan Lotuashvili Dec 27 '14 at 17:58
  • Modified rule and also added explanation with reference links. – anubhava Dec 27 '14 at 18:01
  • @anubhava: I want to redirect only when the request is from android and url does not have string 'api' or 'rest', tried # Android devices RewriteCond %{HTTP_USER_AGENT} Android RewriteCond %{REQUEST_URI} !/pwa RewriteRule ^(.*)$ /pwa/$1 [NC,L] #RewriteRule !^((rest|api)(.*))$ /pwa/$1 [NC,L] but not working , pls help https://stackoverflow.com/questions/55290651/htaccess-do-not-apply-rewrite-rule-for-api-calls – monda Mar 21 '19 at 23:56