Alexander Pozharskii
Alexander Pozharskii

Reputation: 65

Apache htaccess - change subdirectory

My file structure looks like this:

I want to have a redirect from cabinet/file.txt to cabinet/public/file.txt, and if cabinet/public/$filename does not exists then to redirect to cabinet/public/index.php instead.

How can I do this properly?

Upvotes: 0

Views: 32

Answers (2)

anubhava
anubhava

Reputation: 784908

You can use this .htaccess in /cabinet/ folder:

RewriteEngine On
RewriteBase /cabinet/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cabinet/public/$1 -f [NC]
RewriteRule ^(.+?)/?$ public/$1 [L]

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

Upvotes: 1

Mr. Concolato
Mr. Concolato

Reputation: 2230

You can use a rewrite rule in Apache. Here is a good place to start if you are not familiar. What you want looks something like this:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^cabinet/file.txt    cabinet/public/file.txt

ErrorDocument 404 cabinet/public/index.php

Keep in mind that the ErrorDocument will be used for all 404 or files not found.

Upvotes: 1

Related Questions