php - 尝试为 Apache 负载均衡器声明自定义 cookie 时出现错误

标签 php apache .htaccess session cookies

我遵循了许多教程(例如 http://www.markround.com/archives/33-Apache-mod_proxy-balancing-with-PHP-sticky-sessions.html/ ),并设置了一个 Apache 负载均衡器,除了 session 维持之外,它运行良好。

我正在尝试修改我的重定向(直到我添加额外的标志(?):

RewriteRule ^(.*)$ public_html/index.php - [CO=JSESSIONID:balancer.view-a:.ea-hq.com]

这会导致内部服务器错误:

C:/Web Server/Apache24/htdocs/.htaccess: RewriteRule: bad flag delimiters

所以我的 session 不会保留在两台服务器之间。

我在 Windows Server 2013 上使用 Apache 2.4 和 PHP 5.5.11。

我该如何解决这个问题?

如果您需要我提供更多详细信息,请说出来。


我已经做出了改变:

RewriteRule ^(.*)$ - [CO=JSESSIONID:balancer.view-a:.ea-hq.com]
RewriteRule ^(.*)$ public_html/index.php

但现在许多文件无法加载,并且状态为 302“已找到”,并且在通过 Chrome 中的检查器查看时显示为空白。


RewriteRule ^(.*)$ public_html/index.php [CO=JSESSIONID:balancer.view-a:.ea-hq.com]

给了我相同的结果。


这是我的完整 .htaccess 文件,未进行 cookie 修改:

Options +FollowSymLinks
IndexIgnore */*
<FilesMatch "\.(ico|pdf|flv|jpe?g|png|gif|js|css|swf|eot|woff|ttf|svg)$">
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
</FilesMatch>
RewriteEngine On
RewriteBase /

# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f 
# Only use the filename and extension.
RewriteRule ^(?:[^/]+/)*([^.]+\.(?:gif|jpe?g|png|eot|woff|ttf|svg))$ public_html/images/$1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$

RewriteRule ^(.*)$ public_html/index.php

# Use below when apache is upgraded.
#FallbackResource public_html/index.php

#php_flag session.bug_compat_42 1
#php_flag session.bug_compat_warn 0


<IfModule mod_security.c>
SecRuleEngine Off
</IfModule>

最佳答案

RewriteRule 需要 2 或 3 个参数,您的命令有 4 个参数

正确的语法是

  • 命令:重写规则
  • 模式: ^(.*)$
  • 替换: -
  • 标志: [CO=JSESSIONID:balancer.view-a:.ea-hq.com]

该错误仅与语法问题相关,指定替换为 public_html/index.php 和 -(破折号)。

(来自文档)破折号(-)表示不应执行替换(现有路径不受影响地通过)。当需要应用标志而不更改路径时使用此方法。

所以我认为你只需要一个破折号而不是public_html/index.php(你可以分开写其他规则)。

示例

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/index.php/$1 [L,CO=JSESSIONID:balancer.view-a:.ea-hq.com]

apache documentation了解更多信息

关于php - 尝试为 Apache 负载均衡器声明自定义 cookie 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260743/

相关文章:

php - 插入如何进行多对多查询 "OR"两个表

android - Python 系统调用找不到文件,在 Apache 服务器上运行出错

.htaccess - https 通过 htaccess 更改文档根目录

php - Laravel 5 找不到 css 文件

javascript - apache xampp 如何查找内部错误 500 的原因

.htaccess - 在不更改地址栏中的 URL 的情况下使用 .htaccess 重定向 301

php - 未定义索引

php - PHP 的 C++ 中的运算符重载等效,默认输出的 echo/print 类变量

php - MySQL mysqli - 返回数组中的行

javascript - 保持连接活跃