Rewrite Alias ​​Directory

I try to use the Alias ​​folder first to store the project files in a different place than my DocumentRoot , and then execute mod_rewrite in this request. However, it does not seem to be parsing the .htaccess file.

This is the contents of my alias file:

 Alias /test F:/Path/To/Project <Directory F:/Path/To/Project> Order allow,deny Allow from all </Directory> 

This is my .htaccess file:

 Options +FollowSymlinks RewriteEngine on RewriteRule .* index.php [NC] [PT] 

When I delete an alias, everything works fine.

+4
source share
2 answers

mod_alias ALWAYS takes precedence over mod_rewrite. You can never override the mod_alias directive with mod_rewrite.

AliasMatch directive can help you in this case.

+5
source

Here is a solution that some scenarios can solve when you try to use an alias and rewrite, but cannot, because they conflict.

Suppose your DocumentRoot for a specific application is /var/www/example.com/myapp , and you have the following basic directory structure where public requests are either for files in public (for example, a css file) or otherwise routed through index.php .

 myapp/ |- private_library/ |- private_file.php |- private_configs/ |- private_file.php |- public/ |- index.php |- css/ |- styles.css 

The goal is to serve content inside public_webroot , however the URL should be example.com/myapp not example.com/myapp/public .

It might seem like it should work:

 DocumentRoot /var/www/example.com Alias /myapp /var/www/example.com/myapp/public <Directory /var/www/example.com/myapp/public> # (or in this dir .htaccess) RewriteEngine On RewriteBase /public RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [QSA,PT] </Directory> 

But this will lead to an endless loop if you request a URL for a file that does not exist (i.e. one that needs to be routed through index.php ).

One solution is to not use mod_alias, but instead just use mod_rewrite in the root directory of the application, for example:

 DocumentRoot /var/www/example.com <Directory /var/www/example.com/myapp> # (or in this dir .htaccess) RewriteEngine On RewriteRule (.*) public/$1 [L] </Directory> <Directory /var/www/example.com/myapp/public> # (or in this dir .htaccess) RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [QSA,L] </Directory> 

And it's all!

+4
source

All Articles