问题
I'm trying to first use an Alias folder to store my project files in a different location than my DocumentRoot
, and then execute a mod_rewrite
on this request. However it doesn't seem to parse the .htaccess
file.
This is the content 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 remove the Alias everything works fine.
回答1:
mod_alias ALWAYS takes precedence over mod_rewrite. You can never override a mod_alias directive with mod_rewrite.
The AliasMatch directive may help you in this case.
回答2:
Here is a solution that may address some scenarios where you are trying to use alias and rewrite but can't because they conflict.
Suppose your DocumentRoot
for a particular application is /var/www/example.com/myapp
, and you have the following basic directory structure, where public requests are either for files in public
(e.g., a css file), or are otherwise routed through index.php
.
myapp/
|- private_library/
|- private_file.php
|- private_configs/
|- private_file.php
|- public/
|- index.php
|- css/
|- styles.css
The objective is to only serve content inside public_webroot
, however, the URL should be example.com/myapp
not example.com/myapp/public
.
The following 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's .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 infinite loop if you request a URL for a file that doesn't exist (i.e., one that should be routed through index.php
).
One solution is to not use mod_alias, and instead just use mod_rewrite in your application's root directory, like this:
DocumentRoot /var/www/example.com
<Directory /var/www/example.com/myapp>
# (or in this dir's .htaccess)
RewriteEngine On
RewriteRule (.*) public/$1 [L]
</Directory>
<Directory /var/www/example.com/myapp/public>
# (or in this dir's .htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
</Directory>
And that's all!
来源:https://stackoverflow.com/questions/12161198/rewrite-an-alias-directory