regex to not match if a specified string exists
-
20-09-2019 - |
Question
i'm trying to do an apache rewrite where if the term "admin" is contained in the request_uri
mydomain.com/admin/anything_else
re-write the host to use a subdomain
admin.mydomain.com/admin/anything_else.
likewise, if i click a link while in the admin.mydomain.com and it is a url WITHOUT "admin" in it, then i would like to rewrite the url back to
mydomain.com/anything_else
My question is. can regex do an exist check like that? I'm aware that it can do terms not matching (I don't know the syntax though...), but in this case I want regex to fail completely. Any ideas?
ReWriteCond [^(admin)]$
mydomain.com/admin/hi - matches mydomain.com/anything - doesn't match
Solution 3
I ended up doing something similar...
# Redirect to subdomain admin for an admin specific uri request
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} admin
RewriteRule ^(.*)$ http://admin.example.com$1 [L]
# Redirect to subdomain admin for an admin specific uri request
RewriteCond %{HTTP_HOST} ^admin.example.com$
RewriteCond %{REQUEST_URI} !admin
RewriteRule ^(.*)$ http://www.example.com$1 [L]
OTHER TIPS
Try these rules:
RewriteCond %{HTTP_HOST} !=admin.example.com
RewriteRule ^admin(/|$) http://admin.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} =admin.example.com
RewriteRule !^admin(/|$) http://example.com%{REQUEST_URI} [L,R=301]
You may try using negative lookaheads, like mydomain.com/(?!admin)