.htaccess中/ PHP - 重新写,而重新定向到一个不同的URL网址 [英] HTACCESS / PHP - Re-write URL while re-directing to a different URL

查看:122
本文介绍了.htaccess中/ PHP - 重新写,而重新定向到一个不同的URL网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个网站,转诊方案,其中的引荐URL将会像 http://example.com/123456

I am creating a website with a referral scheme, where the referral URL will be something like http://example.com/123456

我想为只显示网址 http://example.com/ ,但仍然捕捉到 123456 并给予重点推荐。

I would like to display the URL as simply http://example.com/ but still capture the 123456 and give the referral point.

难道是可以用的.htaccess的mod_rewrite做到这一点?像

Would it be possible to do this with .htaccess mod_rewrite? something like

RewriteRule ^([0-9]+) ?ref=$1 [L]

将努力转发URL,但有没有办法在这里重定向和显示 http://example.com/ 在地址栏中,没有引用code吗?

would work to forward the URL, but is there a way to redirect here and display http://example.com/ in the address bar, without the referrer code?

这显然是可能的PHP头,但我倒是preFER不使用这个,如果在所有可能的。

It would obviously be possible with PHP Headers but I'd prefer not to use this if at all possible.

推荐答案

这是不可能的,只有RewriteModule一个简单的方法,因为HTTP的是一个无状态的协议。如果删除推荐ID和重定向用户,那么你就不会知道ID再上第二个请求。

This is not possible in a simple way with RewriteModule only, because of HTTP being a stateless protocol. If you remove the referral ID and redirect the user, then you won't know the ID anymore on the second request.

所以,你必须保存ID在Cookie或会话使用PHP和重定向与事后Location头的用户。

So you have to save the ID in a cookie or session with PHP, and redirect the user with the Location header afterwards.

的htaccess

RewriteRule ^([0-9]+) index.php?ref=$1 [L]

的index.php

index.php

if (isset($_GET["ref"])) {
  setcookie("refid", $_GET["ref"]);
  header("Location: http://example.com/");
  exit;
}
if (isset($_COOKIE["refid"])) {
  echo "You came from ref id: $_COOKIE[refid]";
} else {
  echo "You came without a ref id.";
}

这篇关于.htaccess中/ PHP - 重新写,而重新定向到一个不同的URL网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆