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

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

5 Comments

"This webpage has a redirect loop" :/ How to check if URL doesn't contain # ?
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?
And can I add another url to not redirect? /admin and /login for example
Modified rule and also added explanation with reference links.
@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 stackoverflow.com/questions/55290651/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.