Apache rewrites the rule to redirect the entire request to a subdirectory containing other .htaccess and rewrite rules

I have public and private projects on my web server. I put everything that is publicly available at the root of the web server, and I have a private folder that I can only use from the local network (.htaccess is installed there).

I just want to put all the private projects in the private folder and process the requests automatically, but I want the URLs to look like they are served using webroot.
For example, if there is private/project1 , I want to use the URL http://example.com/project1 to serve this folder and do not 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 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 static content will appear, but the links will be broken. What should I change for work?

Also, if I have private/project3 and go to http://example.com/project3/ , there is no problem, but when I go to http://example.com/project3 (without trailing /), the url will be Display as http://example.com/private/project3/ in the browser. What for? How can i avoid this?

+6
source share
3 answers

All you need in your case is mod_alias.

Then do your private project, for example:

 Alias /project3 /apache/htdocs/private/project3 

And with .htaccess you will control access rights.

If you want to manage it without restarting the server, you can try to achieve this using the following configuration, which can be placed in a .htaccess file:

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

index.html - any index file for your project. This way, the public part of the URL will be fully accessible, next to the one you use for private projects. You can also add a RewriteCond to check IP and enable rewriting only for your local network.

+4
source

Actually, looking at your question, it looks like this is a problem with mod_dir interfering with the path pipeline. Specifically, DirectorySlash , which is enabled by default, will 301 redirect the browser if it thinks the browser is requesting a directory and does not have a trailing slash. You can try turning DirectorySlash Off , but there ’s a security warning associated with it :

Disabling trailing slash redirection may result in information disclosure. Consider a situation where mod_autoindex is active (Options + Indexes) and DirectoryIndex is set to a valid resource (say, index.html), and there is no other special handler for this. In this case, a query with a trailing slash displays the index.html file. But the query without a slash listed the contents of the directory.

This may or may not apply to your setup. You can also try changing the rewrite rule for the slash account:

 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)/$ private/$1/ RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*[^/])$ private/$1/ 

But I had mixed results, trying to get mod_rewrite and mod_dir to always play well with each other.

+2
source

Are Virtual Domains Enough?

You can use a domain for private projects, and another for public projects.

http://httpd.apache.org/docs/2.2/vhosts/examples.html

+2
source

Source: https://habr.com/ru/post/921985/


All Articles