I’m trying to redirect with nginx all urls with the subdomain "admin" to www, except for the ones containing "wp-admin".
The behavior should be the following:
–https://admin.domainname.com/slug
=> https://www.domainname.com/slug
–https://admin.domainname.com/wp-admin/slug
=> no redirect
I tried the following one but it’s not working:
rewrite ^(?!/[^/]+/wp-admin/)(/.*)admin(?.*)?$ $1www$2 permanent;
I guess the (/.*)admin(?.*)
part is not correct. How can I capture the "admin" subdomain?
Thanks for your help.
3
Answers
Here is the solution that worked:
This answers only the regex part of the question.
The @IvanShatsky’s answer is more useful as it addresses the the main issue of the OP
Not sure about your exact requirements, but here is a quick fix:
(https|http)
– gets protocol://admin.
– check if there is://admin.
((.(?!/wp-admin/))*)
– gets every char until the end, if it is not followed by/wp-admin/
, fails otherwise.You can’t check domain name with the
rewrite
directive, it works with so-called normalized URI (in your examples it will be/slug
or/wp-admin/slug
). What you can do is to checkHost
HTTP header value: