【问题标题】:Apache Rewrite rules not working as expectedApache Rewrite 规则未按预期工作
【发布时间】:2013-07-24 17:18:21
【问题描述】:

我有两个 apache 服务器都具有相同的设置,我克隆了 apache 配置文件,并且只更改了 ServerName 部分。当我键入 mysite.com/somestuff 它应该重写为 index.php 它在我的旧服务器上执行它,但不是我的新服务器。当我执行 mysite.com/index.php/somestuff 时,我已确保 .htaccess 存在,但就像我的第一个站点一样,我需要它与 mysite.com/somestuff 一起使用。

我的 .htaccess 和 apache 配置文件真的是我的头撞墙了

#.htaccess file
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule .? %{ENV:BASE}index.php/ [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

现在我的 apache 配置

<VirtualHost *:80>
        ServerAdmin user@host.com
        ServerName mysite.com
        DocumentRoot /home/richardw/www/halogen/web
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /home/richardw/www/halogen/web/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

如果这是转发,我深表歉意,但我一直在寻找,我即将失去它。

【问题讨论】:

  • 当你转到http://mysite.com/时,你是否被重定向到http://mysite.com/index.php/

标签: apache .htaccess zend-framework mod-rewrite ubuntu


【解决方案1】:

当你转到http://mysite.com/时,你是否被重定向到http://mysite.com/index.php/

如果发生这种情况,这意味着 mod_rewrite 没有加载到您的新服务器中。您需要确保它已加载到您的 apache 的服务器配置文件中。请参阅 this answer 了解有关 apache 如何工作的一些说明。

重定向起作用的原因是因为这个容器:

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

这实质上是说“如果 mod_rewrite 加载”,那么如果 mod_alias 已加载,它将根请求重定向到 /index.php/。因此,如果发生重定向,mod_rewrite 将加载。

【讨论】:

  • 是的,就是这样:/。我发誓我这样做了,但我错过了打字。 ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
【解决方案2】:

您是否尝试评论或删除此行
Options +FollowSymlinks
在您的 .htaccess 中?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 2013-08-11
    • 2019-03-15
    • 1970-01-01
    • 2014-07-15
    • 2020-01-12
    • 2020-10-30
    • 2016-06-10
    相关资源
    最近更新 更多