Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rewrite rule to redirect all request to subdirectory containing another .htaccess and rewrite rules

I have public and private projects on my webserver. I put everything what is public into the webserver root, and I have a private folder there which I can only reach from local network (set by .htaccess in there).

I want to simply put every private projects in the private folder and handle the requests automatically, but want the URLs look like they are served from webroot.
For example if there is private/project1 I want to use the URL http://example.com/project1 to serve that folder and don't want to change the URL.

This simple rewrite:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ private/$1           

works, but when I have a private/project2 with another .htaccess:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project2/

<Files .*>
       Order Deny,Allow
       Deny From All
</Files>

# Allow asset folders through
RewriteRule ^(assets/.+) - [L]

# Protect files from being viewed
RewriteRule ^(uploads.+) - [F,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>
Options -Indexes

then the static content will appear, but the links are broken. What should I modify to work ?

Also if I have a private/project3 and browse to http://example.com/project3/ there is no problem, but when I browse to http://example.com/project3 (without the trailing /) the URL will be visible as http://example.com/private/project3/ in the browser. Why ? How can I avoid that ?

like image 835
kissgyorgy Avatar asked Aug 02 '12 19:08

kissgyorgy


1 Answers

All you need in your case is mod_alias.

Then serve your private project like:

Alias /project3 /apache/htdocs/private/project3

And with .htaccess you will control access rights.

If you want to control it without restarting server, you can try to achieve this with following config, that can be placed in .htaccess file:

RewriteRule ^/(project1)$ /private/$1/index.html
RewriteRule ^/(project1/)(.*)$ /private/$1$2

index.html - any index file for your project. This way public part of URL's will be completly accessible, beside path's you are using for the private projects. You also can add RewriteCond to check IP and enable rewriting only for your local network.

like image 52
Nagh Avatar answered Sep 28 '22 08:09

Nagh