具有别名的Apache虚拟主机

我有一个运行在我的Apache本地副本上的虚拟主机。 它有一个别名

<VirtualHost 127.0.0.1> ServerName oliverash.me.dev DocumentRoot "/usr/docs/oliverash.me/public" Alias /mayor "/usr/docs/uni/tmosct/public" </VirtualHost> 

这工作正常。 我可以访问别名,在我的浏览器中转到oliverash.me.dev/mayor 。 不过,我有一个简单的.htaccess rewrite该别名的文件夹,它将所有内容(文件夹除外)重写到index.php页面。 这个重写工作正常。

虽然重写正在工作,但重写将返回404 Not Found错误。 例如,如果我去到oliverash.me.dev/mayor/abcdefg ,重写到index.php文件,但我得到这个错误:

“请求的URL /usr/docs/uni/tmosct/public/index.php在此服务器上找不到”。

这个文件确实存在。 我可以直接访问它。

当我使用相同的别名,而不是localhost主机,而不是只是这个虚拟主机,重写工作正常,和index.php返回与实际文件,而不是404。这使得我认为这是关系到权限我的虚拟主机。 所以我试图在我的httpd-vhosts.conf下添加这个(没有运气):

 <Directory "/usr/docs/oliverash.me/public"> AllowOverride All Order allow,deny Allow from all </Directory> 

这些是在localhostDocumentRoot上使用的相同的设置,所以我不明白为什么它没有帮助。

更新:

所以我查了一下Apache的error_log。 原来,这个别名的重写实际上是相对于这个虚拟主机的文档根。

[Fri Jan 06 22:30:57 2012] [error] [client 127.0.0.1] File does not exist: /usr/docs/oliverash.me.dev/usr

这个重写实际上是在/usr/docs/oliverash.me.dev/public/usr/docs/uni/tmosct/public for index.php。 希望它显然应该在/usr/docs/oliverash.me.dev

更新2:

好的,这是一个我遇到Apache的本地副本的问题。 但是,我刚刚在我的实时Web服务器上遇到与别名相同的问题。 这不是使用任何一种虚拟主机。

[Sat Jan 07 02:41:51 2012] [error] [client 86.11.223.135] File does not exist: /var/www/html/var

再次,path是相对于DocumentRoot。 这应该是一个绝对的path。

有点烦人:(

更新3:

这正是我遇到的问题,但也许更好措辞: http : //forums.devshed.com/apache-development-15/rewritebase-alias-and-rewriterule-are-not-rooting-to-required-file -395917.html

解决scheme :RewriteBase必须与Alias定义相同,而不是文件系统中的物理path/目录!

检查mod_rewrite的RewriteBase指令,听起来好像可能与此有关。 请阅读http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

编辑1
为了试试这个,我在VMware中安装了一个干净的新Ubuntu,一个干净的apache2安装,并创build了一个新的文件夹出正常的path,然后设置一个.htaccess和我使它适当的RewriteBase规则工作。 默认的DocumentRoot是/ var / www,我在里面放了一个index.php来显示我的位置。 它回应出“我是默认的index.php!”。 然后我在Apachesconfiguration中创build了这个别名:

 Alias /testberg /home/www/testberg <Directory "/home/www/testberg"> Options +Indexes AllowOverride All </Directory> 

在那里,我又放了一个index.php,上面写着“我是testberg的index.php”。 在/ home / www / testberg下我创build了一个带有以下内容的.htaccess:

 RewriteEngine On RewriteBase /testberg RewriteRule ^apa.test$ index.php 

当我浏览到http://192.168.100.183/testberg/apa.test时,我现在看到:“我是testberg的index.php!&#x201D; 在Apaches logfile中没有错误等

这不是你想要完成的吗?

编辑2
尝试使用不同的虚拟主机。 在我的Windows桌面上,我将ahntest.net指向我的VMware IP,位于c:\windows\system32\drivers\etc\hosts 。 在我的VMware服务器上,我创build了/home/www/ahntest.net并在/home/www/ahntest.net放置了一个修改过的index.php来回显“我是ahntest.net中的index.php!” 并创build了以下虚拟主机:

 <VirtualHost *:80> ServerAdmin webmaster@localhost ServerName ahntest.net DocumentRoot /home/www/ahntest.net Alias /testberg /home/www/testberg <Directory "/home/www/testberg"> Options +Indexes AllowOverride All </Directory> </VirtualHost> 

浏览http://ahntest.net给我“我是在ahntest.net的index.php!”,浏览http://ahntest.net/testberg给我“我是testberg的index.php&#xFF01; 最后浏览到http://ahntest.net/testberg/apa.test给我“我是testberg的index.php!&#x201D; 所以这里的工作也很好,我可以告诉。 编辑2中的.htaccess /补丁与上面编辑1中的相同。

你需要的是别名目标的目录容器

 <Directory "/usr/docs/uni/tmosct/public"> ... </Directory> 

UPDATE

您必须将Directory容器放在VirtualHost容器中,以便它们为虚拟主机工作。

更新2

快速检查jsFiddle的东西,你需要一个

 NameVirtualHost *:80 

在容器上面,接下来你应该重写

 <VirtualHost 127.0.0.1> 

 <VirtualHost *:80> 

最后一个重要的是你需要在你的hosts文件中input一个条目,这样URL就可以parsing了。