Apache .htaccess在使用别名URI时被忽略
我正在使用mod_rewrite将所有请求路由到index.php 我的文件夹结构如下:Apache .htaccess在使用别名URI时被忽略,apache,.htaccess,mamp,Apache,.htaccess,Mamp,我正在使用mod_rewrite将所有请求路由到index.php 我的文件夹结构如下: /Users/Peter/Projects/Framework /.htaccess /index.php 在.htaccess文件中,我有以下内容: RewriteEngine on RewriteCond $1 !^index\.php/ RewriteRule ^(.*)$ index.php/$1 [L] 当我访问projects.localhost/framework/exam
/Users/Peter/Projects/Framework
/.htaccess
/index.php
在.htaccess文件中,我有以下内容:
RewriteEngine on
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]
当我访问projects.localhost/framework/example
时,这很好用
我还设置了以下别名:
别名/~Alias/Users/Peter/Projects
当我导航到http://projects.localhost/~alias/framework/example
我收到一个404错误,错误日志中有以下内容:文件不存在:/Users/Peter/Projects/framework/example
当我使用别名URL时,似乎没有调用.htaccess文件(在.htaccess文件中键入giberish不会触发任何类型的错误,当使用别名URL时,似乎可以确认这一点)
AllowOveride设置为All
如何在使用别名URL时使.htaccess正常工作,并使重写规则在不考虑URL(别名或非别名)的情况下始终适用
编辑:导航到
projects.localhost/~alias/framework/index.php/example
也可以正常工作,确认别名工作正常(除了没有应用.htaccess规则)。您确定启用了mod_rewrite吗?我发现您的问题也存在同样的问题。从@Gerbens的评论中,我找到了Apache手册中的以下内容:
#
# /abc/def/.htaccess -- per-dir config file for directory /abc/def
# Remember: /abc/def is the physical path of /xyz, i.e., the server
# has a 'Alias /xyz /abc/def' directive e.g.
#
RewriteEngine On
# let the server know that we were reached via /xyz and not
# via the physical path prefix /abc/def
RewriteBase /xyz
# now the rewriting rules
RewriteRule ^oldstuff\.html$ newstuff.html
在别名目录中的.htaccess文件中添加重写基解决了我的问题
那么你的情况呢
RewriteEngine on
RewriteBase /~alias
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]
Alias似乎绕过了普通根目录,因此将.htaccess文件放在那里。您可以将另一个.htaccess文件放在
/Users/Peter/Projects
文件夹中。我已尝试将一个.htaccess文件放在Projects文件夹中,但这对别名URL也没有影响。(它确实会影响无别名的url。)如果可以的话,我会投两次票——保存我的培根!谢谢,@danneth。太好了,谢谢。因此,如果没有RewriteBase,Apache会愚蠢地假设它是从物理路径访问的。我想知道为什么这种行为没有被报告为bug。