Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect if url doesn't contain some string

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

like image 252
Levan Lotuashvili Avatar asked Sep 17 '25 21:09

Levan Lotuashvili


1 Answers

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:

  • Apache mod_rewrite Introduction
  • Apache mod_rewrite Technical Details
like image 93
anubhava Avatar answered Sep 19 '25 14:09

anubhava