.HTACCESS and Redirecting to new domain with conditioning

2

I have a WordPress web portal hosted on a professional server that responds to URLs:

  • www.portalweb.subdominio.cu
  • www.portalweb.cu
  • This is due to a reverse proxy.

    I managed to redirect the traffic with .htaccess, but I have not achieved what I want, which is the following:

    Redirect all traffic from (URL 1) to (URL 2) except the WordPress backend so that all visitors can access via (URL 2) and admins and publishers can access the backend via (URL 1).

    • Visitors access through www.portalweb.cu
    • Editors access by www.portalweb.subdominio.cu/backend

    Any ideas on how it could be done?

        
    asked by Leonardo Expósito Rojas 17.02.2018 в 19:19
    source

    1 answer

    0

    For these 2 redirects:

  • www.portalweb.subdominio.cu (no /backend ) → www.portalweb.cu

  • www.portalweb.cu/backendwww.portalweb.subdominio.cu/backend


  • There are 2 conditions to check: what is the host and what is the route.
    All RewriteCond are conditions that, if met, allow the following RewriteRule to be executed (if it also matches).

    RewriteEngine On
    RewriteBase /
    
    # Todo excepto backend a www.portalweb.cu
    RewriteCond %{HTTP_HOST}     ^www\.portalweb\.subdominio\.cu$   [NC]
    RewriteCond %{REQUEST_URI}  !^backend(?:/|$)   [NC]
    RewriteRule (.*)             http://www.portalweb.cu/$1   [R,L]
    
    # www.portalweb.cu/backend a www.portalweb.subdominio.cu/backend
    RewriteCond %{HTTP_HOST}          ^www\.portalweb\.cu$   [NC]
    RewriteRule ^(backend(?:/.*|$))   http://www.portalweb.subdominio.cu/$1   [NC,R,L]
    

    These rules are quite obvious. The flags mean:

    • [NC] No Case. Ignore uppercase and lowercase.
    • [L] Last. Do not keep trying more rules.
    • [R] Redirect. Redirect to another URL (instead of rewriting). This flag makes a temporary redirect (302). If you are interested in a permanent, after you have verified that all works well, you can change it by [R=301] .
    answered by 19.02.2018 в 22:59