0

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/

Tom
  • 1
  • 2
  • Your RewriteRule pattern only matches a completely empty path, or one that consists of `index.php` only. `test2/` fulfills neither of those two conditions. – CBroe Feb 02 '23 at 14:42
  • Hi, thats correct, I've tried something like this `RewriteRule ^(index\.php)?(.+)$ /test/$1 [L]` but it results in an internal server error 500 – Tom Feb 02 '23 at 22:21
  • Try `RewriteRule ^test/ - [L]` (do nothing and end current round of rewriting, when the path already starts with `test/`), followed by `RewriteRule (.*) /test/$1 [L]` – CBroe Feb 03 '23 at 06:46
  • Hi, sorry I misinterpreted that, it seems so work, the only thing is, its now an external redirect instead of an internal. So I’ve added `(index\.php)?` And tried this: `RewriteRule ^test/ - [L] RewriteRule (index\.php)?(.*) /test/$1 [L]` But now this redirects every request to `/test/` !? – Tom Feb 05 '23 at 22:43
  • Ok I've finally got it: `RewriteCond %{REQUEST_URI} !^/test/ RewriteRule (.*) /test/%1 [L]`. This works, and it's not about the index\.php its the slash at the end ;) – Tom Feb 06 '23 at 00:09

1 Answers1

0

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;
}
Tom
  • 1
  • 2