Htaccess redirect if url does not contain some string
I want to redirect all incoming requests to a different url if it doesn't contain # and admin. I need this 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/ * won't redirect
+3
source to share
1 answer
This code can be used in a file DOCUMENT_ROOT/.htaccess
:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^((admin|login)(/.*)?)?$ /#%{REQUEST_URI} [L,NC,NE,R=302]
Also remember that the webserver is not part of the URL after #
as this is only allowed on the client side.
-
RewriteCond %{REQUEST_FILENAME} !-f
skips this rule for all files. - Usage
!
overrides whole match in patternRewriteRule
-
((admin|login)(/.*)?)?
matches whatever is/admin/
or/login/
OR emptry (landing page) - If negation is true, this rule redirects it to
/#%{REQUEST_URI}
where%{REQUEST_URI}
the original URI represents.
Links:
+3
source to share