htaccess的从WWW到非www会话/饼干带来变数变数、饼干、htaccess、WWW

2023-09-02 00:51:36 作者:灞道尛侽貹

.htacces重定向从 www.example.com example.com (同​​一域加www。)< BR> 回访者可能在用户代理一个 visitor_id 的cookie。 我希望把这个值通过一个cookie或会话中的域。 我想这一点,但Cookie是WWW的域名

.htacces redirects from www.example.com to example.com (same domain without www.) Returning visitor could have in user-agent a visitor_id cookie. I want to bring this value through domains within a cookie or session. I tried this, but the cookie is created for the www domain

RewriteCond %{HTTP_HOST} ^www.example.com
RewriteCond %{HTTP_COOKIE} visitor_id=([^;]+)
RewriteRule .* - [C,env=foo:%1]
RewriteRule ^(.*) http://example.com [L,R=301]
Header set Set-Cookie "visitor_id=%{foo}e; path=/" env=foo

此外,该环境变量适用于本地主机(阿帕奇2.4.2,Win32的),但网上(阿帕奇2.2.25,Linux)在cookie中的值%{}富E,而不是预期的数量。

Moreover the environment variable works on localhost (Apache 2.4.2, Win32), but online (Apache 2.2.25, linux) the value in cookie is "%{foo}e" instead of expected number.

也试过用 mod_session_cookie 但无法找到实际的例子。

Also tried with mod_session_cookie but can't find practical examples.

如何通过域,把 visitor_id 在Cookie或会话Cookie重定向?

How redirect through domains, bringing visitor_id in a cookie or session cookie?

推荐答案

一直以来,环境%{ENV} 变量没有始​​终如一地不同的Apache版本的表现,我建议设置的的cookie 的用重写规则本身使用 [CO] 的饼干的标志。

Since, the environment %{env} variables are not behaving consistently across different Apache versions, I suggest setting the cookie with a RewriteRule itself using the [CO] cookie flag.

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteCond %{HTTP_COOKIE} visitor_id=([^;]+) [NC]
RewriteRule .* $0/vid/%1 [C] # Appends the cookie value to the URL
RewriteRule ^(.*)/vid/(.*)$ http://example.com/$1 [L,R=301,CO=visitor_id:$2:.example.com:14400:/]

下面是你的的.htaccess 文件所做的更改列表:

Here are the list of changes made to your .htaccess file:

的RewriteCond 匹配是不区分大小写的,现在(使用 [NC]

RewriteCond matches are case-insensitive now (using [NC])

%{HTTP_HOST} 的条件已经越狱 (。匹配任何字符,否则)

The dots in %{HTTP_HOST} condition have been escaped . ( . matches any char otherwise)

第一个重写规则追加访客ID(捕获为%1 )的网址(捕获为 $ 1,0

The first RewriteRule appends the visitor id (captured as %1) to the URL (captured as $0)

最后一个重写规则解析URL中的访客ID(如 $ 1 ),并进行永久 [R = 301] 重定向到 http://example.com 一起写了一个名为cookie的 visitor_id 使用 [CO] 标记。

The last RewriteRule parses the visitor id from the URL (as $1) and performs a permanent [R=301] redirect to http://example.com along with writing a cookie named visitor_id using the [CO] flag.

的语法中的的cookie 的重写标志如下:

The syntax of the cookie rewrite flag is as follows

[CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly]

在那里指定值的名称的值和域的是强制性的。在一辈子的默认设置为 0 这意味着该Cookie持续仅在当前浏览器会话。 路径的默认设置为 / ,安全和仅Http 到

where specifying values for name, value and domain is mandatory. The lifetime defaults to 0 which means the cookie persists for the current browser session only. Path defaults to /, secure and httponly to false.

[CO] 用标志指定的域的是 .example.com的等等该饼干的下 example.com 域将成为访问的所有主机。在一辈子的被指定为 14400 这是在几分钟内,因此达 10 天。

The [CO] flag used specifies domain as .example.com so that the cookie becomes accessible to all the hosts under example.com domain. The lifetime is specified as 14400 which is in minutes and so amounts to 10 days.