Home > OS >  Redirecting all links to directory
Redirecting all links to directory

Time:12-25

I redirect all link to "public" directory width:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [L,QSA,NC]

but it gives me always "500 Internal Server Error"

if i write concrent path it will work

RewriteEngine on
RewriteBase /

RewriteRule ^(.*)$ /public/page/

but if I put dynamic variable $i it give me 500 error

CodePudding user response:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [L,QSA,NC]

This will result in a rewrite-loop (500 error) unless it rewrites to a physical file. You need to ensure you only rewrite direct requests. You can do this by checking against the REDIRECT_STATUS env var.

For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) public/$1 [L]

The QSA and NC flags are not required here.

OR, use the END flag instead on Apache 2.4:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) public/$1 [END]
  • Related