我们的网络服务器结构如下
/path-to-docdir [Contains main pages]
./app/ [Some application not directly related to the main content]
主要页面服务良好。在应用程序目录中,我尝试使用 mod_rewrite 将所有页面重定向到公共(public)目录中的 index.php
所以请求
/server/app/start/login/ => /server/app/public/index.php
/server/app/start/login => /server/app/public/index.php
/server/app/start/login?name=someone => /server/app/public/index.php?name=someone
前端 Controller 依赖于REQUEST_URI
字符串的最后两个组成部分来解释 Controller 和操作。
为了执行上述操作,我在 app
目录中创建了以下 .htaccess
文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
在这里,我发现服务器进入了上述 URL 的重定向循环。我尝试用 index.php
替换 $1
,这确实打开了该页面,但页面内对脚本文件的内部引用遇到了错误。
经过一番搜索后,我添加了以下行:
RewriteBase /app/
这似乎没有任何区别。我还应该注意,如果我将应用程序目录设置为服务器根目录,一切都会按预期工作。
我还打开了调试日志记录,我从中所能收集到的信息是服务器确实进入了重定向循环。
[perdir /path-to-docdir/app/] internal redirect with /app/public/public/public/public/public/public/public/public/public/public/public/start/login/ [INTERNAL REDIRECT]
I'm not really sure where to start looking at this point, so any help or pointers to move me further would be much appreciated.
Edit:
This htaccess file worked for me - if anyone else is looking for a solution. Apparently, I was missing rewrite conditions
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php?$1 [L]
</IfModule>
最佳答案
几点:
RewriteBase
路径不需要尾部斜杠,因此 RewriteBase/app/
应为 RewriteBase/app
。
RewriteRule ^(.*)$ public/$1
使 RewriteRule ^$ public/
变得多余。前者将匹配 http://yourserver/app
并在内部重写为 http://yourserver/app/public
,因此不需要后者。
最后,尝试添加重写条件:
RewriteEngine On
RewriteBase /app
RewriteCond %{REQUEST_URI} !^/app/public/
RewriteRule ^(.*)$ public/$1 [L]
关于apache - mod_rewrite 在子目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11143284/