Reputation: 1112
I want set a subfolder as the root folder, what means rewrite every requested URL by appending a directory before their root, like
example.com/cat/ -> example.com/public/cat/
example.com/cat/black/ -> example.com/public/cat/black/
example.com/public/cat/ -> example.com/public/public/cat/
I've tried
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule (.*) /public/$1 [L]
But when I requested any of its page, I got Internal Server Error.
Upvotes: 1
Views: 325
Reputation: 74018
The rule is correct so far. The internal error comes from an endless rewrite loop.
When you request example.com/cat
, it will be rewritten to example.com/public/cat
. But this new URL is fed back into the rewrite module and again, example.com/public/cat
is rewritten to example.com/public/public/cat
, which is rewritten to example.com/public/public/public/cat
, and so on, until some internal counter is exceeded and Apache reports an internal error.
To fix this, you must add a condition, which tells Apache to stop rewriting any further. For this, you can use one of the variables, which are set by Apache, when a rewrite occurs.
RewriteCond %{REDIRECT_STATUS} ^$
RewriteRule (.*) /public/$1 [L]
This checks, if REDIRECT_STATUS
is empty (^$
). When it is empty, do the rewrite. After the rewrite, REDIRECT_STATUS
will be set and not empty, and as a consequence no further rewrites will be done.
If you wouldn't rewrite
example.com/public/cat/ -> example.com/public/public/cat/
you could also check, if the requested URL doesn't start with public
, e.g.
RewriteRule !^public/ /public%{REQUEST_URI} [L]
Upvotes: 1