htaccess和重写 – 内部redirect在别名

我有一个子目录下面的.htaccess

 RewriteEngine on RewriteRule ^file.js$ file.php 

包含上述和其他文件的目录如下所示:

 dir/ ├── index.html └── js ├── file.php └── .htaccess 

我想我的file.js内部redirectfile.php

当我在/var/www/html/ dir下finddir ,我的vhost定义如下所示:

 DocumentRoot /var/www/html <Directory "/var/www/html"> AllowOverride All Require all granted </Directory> #Alias /dir "/tmp/dir" #<Directory "/tmp"> # AllowOverride All # Require all granted #</Directory> 

它的作品

但是,当我将dir移动到/tmp/并将vhost更改为如下所示:

 DocumentRoot /var/www/html #<Directory "/var/www/html"> # AllowOverride All # Require all granted #</Directory> Alias /dir "/tmp/dir" <Directory "/tmp"> AllowOverride All Require all granted </Directory> 

redirect不再工作了 ,虽然我仍然可以访问其他文件,如index.html

日志告诉我,重写preprends DocumentRoot到我的file.php的完整path,因此,给我一个文件不存在消息

题:

如何编写一个不知道放置位置的.htaccess文件,并且只是从一个特定的文件redirect到另一个文件?

请记住,虽然我能够更改主要的vhostconfiguration来处理这个问题, 我的目标是只使用.htaccess来做到这一点

最佳做法是将configuration指令移至实际的Apacheconfiguration文件(并完全禁止.htaccess文件)。

在这种情况下,这将解决您的问题,因为您不再依赖文件系统位置为您的指令生效…

 <VirtualHost *:80> ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example.com/ Alias /scripts/ /var/www/scripts/ RewriteEngine on RewriteRule ^file.js$ file.php </VirtualHost> 

如果您不是系统pipe理员,并且没有访问您的Apacheconfiguration,那么您的问题不会在主题上,但这不是问题的关键,您可以通过创build(复制) .htaccess文件来解决问题包含file.js目录。 根据AllowOverride 指令为您configuration真正的pipe理员,您通常可以将.htaccess文件放置在Apache有权访问的任何目录和子目录中。 您不限于DocumentRoot。