apache - mod_rewrite 在子目录中

标签 apache mod-rewrite

我们的网络服务器结构如下

/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/

相关文章:

ruby-on-rails - 如何禁用 HTTP 严格传输安全?

linux - 无法从具有不同端口的互联网访问部署在 centOS 上的我的 meteor 应用程序

java - 从 CGI 脚本调用 Java 程序失败

apache - 如何从 https 子域 URL 中删除 www

.htaccess - 使用 Htaccess 文件创建 SEO 友好的 URL

php - Apache mod-rewrite htaccess - 带参数的动态 url

php - Linux Apache2 PHP 5.3 日志文件的正确权限是什么?

django - 错误 apache 配置错误 : couldn't perform authentication. AuthType 未设置!:/在 centos 6

wordpress - htaccess 将domain2.com重定向到domain1.com/specific-page的 final方法

.htaccess - 仅使用 .htaccess 在某些页面上将 http 重写为 https