.htaccess错误500在生产中,而不是在开发中。环境变量

时间:2014-04-15 10:49:44

标签: apache .htaccess mod-rewrite rewrite environment-variables

我的网站在我的本地开发环境中运行良好,但在生产中使用HTTP Error 500: Internal server error进行了轰炸。我无法访问apache error_log,因为它位于共享托管环境中。

我认为问题是在.htaccess中设置apache环境变量 - 可能导致某种无限循环?我需要这些,因为它们被PyroCMS选中并用于确定特定于环境的设置,例如DB config。

<IfModule mod_rewrite.c>

    # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    # disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
    Options -MultiViews
    RewriteEngine on

    # Automatically determine and set the PYRO_ENV variable
    RewriteCond %{HTTP_HOST} ^(localhost|local.mydomain.com|mydomain|mydomain.local)$ 
    RewriteRule (.*) $1 [E=PYRO_ENV:development,L]

    RewriteCond %{HTTP_HOST} ^staging.mydomain.com$
    RewriteRule (.*) $1 [E=PYRO_ENV:staging,L]

    RewriteCond %{HTTP_HOST} ^www.mydomain.com$
    RewriteRule (.*) $1 [E=PYRO_ENV:production,L]

    # Rewrite "domain.com -> www.domain.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteCond %{HTTP_HOST} (.+)$ [NC]
    RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,E=PYRO_ENV:production,L]

    # Keep people out of codeigniter directory and Git/Mercurial data
    RedirectMatch 403 ^/.*/(system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg|db).*$

    # 301 permanent redirects for old web site pages
    RewriteRule ^some/old/path$ /some/new/path [R=301,L]
    RewriteRule ^some/other/old/path$ /some/other/new/path [R=301,L]

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    <IfModule mod_php5.c>
            RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
            RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
</IfModule>

如果我注释掉以下几行:

 RewriteCond %{HTTP_HOST} ^www.mydomain.com$
 RewriteRule (.*) $1 [E=PYRO_ENV:production,L]

然后错误500消失,而我得到数据库连接错误(因为PyroCMS无法检测到环境)。

通常,我会使用SetEnv指令将环境变量直接设置到vhost配置中。但是,由于此特定站点位于共享主机上,因此我无法访问这些站点。

如何解决此问题?

另一方面,我在下面的部分提供旧网站页面的301重定向到新网站(这是旧网站的重建)。我的直觉告诉我,即使我解决了这个问题,当.htaccess文件中的另一个RewriteRule进一步匹配时,设置在.htaccess文件顶部附近的环境变量将会丢失。我这个想法是否正确?

1 个答案:

答案 0 :(得分:0)

解决方案涉及从设置环境的L中删除RewriteRule标志。 L标志阻止了进一步处理规则并调用无限循环。通过删除L标志,这意味着环境已设置并适用于所有进一步的重定向。这也回答了我关于能够在另一次301重定向后持续ENV的问题。

# Automatically determine and set the PYRO_ENV variable
# We DON'T use the L flag on RewriteRule here as we want it to persist through redirects.
RewriteCond %{HTTP_HOST} ^(localhost|local.mydomain.com|mydomain|mydomain.local)$ 
RewriteRule (.*) $1 [E=PYRO_ENV:development]

RewriteCond %{HTTP_HOST} ^staging.mydomain.com$
RewriteRule (.*) $1 [E=PYRO_ENV:staging]

RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule (.*) $1 [E=PYRO_ENV:production]