Search code examples
.htaccessurl-rewritingxamppsubdirectory

.htaccess URL internal Rewrite to (all)-Subdirectory


I'm currently have this:

RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php)?$ /test/ [L]

(index.php seems to be necessary, it's from here: How can I use .htaccess rewrite to redirect root URL to subdirectory?)

System: MacBook Pro - XAMPP 8.1.2 (installer not VM)

I’m trying to rewrite not redirect (not changing adressbar), everything to /test/

This is working:

Typing in:

localhost/

should show content of

localhost/test/

This is not working:

with all subdirectories localhost/test2/ -> localhost/test/test2/


Solution

  • Ok this works (as far as I understand 🙈):

    1. Prevent endless redirecting and getting path (with file (complete
      URI))
    RewriteCond %{REQUEST_URI} !^/subdirectory/
    
    1. Rewrite everything to /subdirectory/ + requested path/file
    RewriteRule (.*) /subdirectory/%1 [L]
    

    %1 get's URI from RewriteCond.

    That's necessary 'cause (.*) won't give us the whole URI (path + index.html ...), it's just the path.



    And the same for nginx:

    if ($uri !~ "^/subdirectory/"){
        rewrite /(.*) /subdirectory$uri last;
    }