Search code examples
apacheurlhttp-redirecturl-rewritingurl-routing

How to rewrite URL paths in Apache to avoid infinite loop


I'm trying to set some rewrites rules on my VirtualHost. I try to redirect root '/' to a specific path '/mypath'. An a path '/foo' to root '/' without enter in a loop.

RewriteEngine On
RewriteCond %{HTTP_HOST}%{REQUEST_URI} =server.domain.com/foo
RewriteRule /foo / [R,E=DEV:true]
RewriteCond %{HTTP_HOST} =server.domain.com
RewriteCond %{ENV:DEV} !^true$
RewriteRule ^/$ /mypath [R]

I'm expecting that if the user ask for:

server.domain.com/foo he/she goes to server.domain.com

And if the user ask for:

server.domain.com he/she goes to server.domain.com/mypath

As you could see on my code I set an environmental variable to check If I have to rewrite or not, but isn't working.


Solution

  • This problem was solved using two domains.

    We create: server.domain.com and server-aux.domain.com

    The rewrite rules looks like:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^server-aux.domain.com$
    RewriteRule ^(.*)$ https://server.domain.com/foo [R=301]
    

    Now server.domain.com goes to / and server-aux.domain.com goes to /foo