Search code examples
.htaccesshttp-redirectinternal

multiple internal redirects to different indexes in folders


I'm new to htaccess and I'm searching for a way to get an internal redirect to set 2 URLs to different folders.

example.com/cms/ and everything behind the /cms/ needs to go to
rootfolder -> cms

example.com/(the rest of the pages)
needs to go to
rootfolder -> public


Solution

  • Try addng this to your htaccess file in your document root:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/cms
    RewriteCond %{REQUEST_URI} !^/public
    RewriteCond %{REQUEST_URI} !-f
    RewriteCond %{REQUEST_URI} !-d
    RewriteRule ^(.*)$ /public/$1 [L]
    

    Essentially:

    • If the request is for /cms, let it get resolved to /cms directly
    • If the request is already for /public, let it get resolved directly
    • If the request is for an existing file or directory, let it resolve
    • Otherwise, rewrite the request appending a /public in front of the URI