VNP Community
Tags
This content has not been tagged yet
> Hướng dẫn tạo trang redirect theo tình huống, Use 301 redirect  2 Pages V   1 2 >  
Reply to this topicStart new topic
PETER
post Feb 21 2008, 03:49 AM
Post #1


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Một số thủ thuật nhỏ PETER share cho các bạn, tạo trang redirect theo tình huống.

1. Reditect theo referer: Tức là ta set nếu request từ 1 domain cho trước thì redirect đến trang A, tất cả request còn lại thì redirect tới trang B dựa vào script check referer của PHP. Không biết bạn hình dung ra chưa nhưng xem đoạn script dưới đây thì bạn sẽ nhận ra thôi 3.gif

CODE
<?php
$referer=$_SERVER["HTTP_REFERER"];
$referer=parse_url($referer);
$referer=$referer['host'];
$referer=str_replace("www.","",$referer);

    if ($referer=="vnpower.org") {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.vnpower.org/web-tools/" ); }
    else {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.vnpower.org/" ); }
?>

Với đoạn script trên thì mọi request từ domain vnpower.org sẽ được chuyển đến http://www.vnpower.org/web-tools/ và những request bất kỳ còn lại sẽ được chuyển đến domain gốc http://www.vnpower.org

VD: Test trên www3.vnpower.org
- Bạn thử đặt 1 link tới http://www3.vnpower.org hoặc enter trên trình duyệt cũng được, khi đó bạn sẽ được redirect tới http://www.vnpower.org nhưng nếu bạn click vào link của www3.vnpower.org ngay tại diễn đàn này thì bạn sẽ được redirect tới http://www.vnpower.org/web-tools/
+ PETER nghĩ ra cách này vì trên diễn đàn có một số link đến VNP Web Tools cũ ở www3, PETER muốn chuyển tất cả request đó đến tương ứng ở VNP Web Tools mới.
+ Link tới www3: http://www3.vnpower.org

Các tình huống tiếp theo sẽ được giới thiệu vào lúc khác.
Go to the top of the page
 
+Quote Post
Game Online
blackeye87
post Feb 21 2008, 05:19 PM
Post #2


Yahoo! Playa
Group Icon

Group: Member
Posts: 137
Cash: 16k
Age: N/A
Joined: 14-January 07
Member No.: 20,857
Yahoo! Status: N/A



Anh PETER có thể post bài hướng dẫn redirect theo IP được ko?Bài này em đang cần 4.gif ! Thanks anh nhiều !
Go to the top of the page
 
+Quote Post
PETER
post Feb 21 2008, 05:24 PM
Post #3


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Redirect theo địa chỉ IP cho trước hay sao em?

Nếu địa chỉ IP cho trước thì chỉ đơn giản như sau:

CODE
<?php
$ip=$_SERVER["REMOTE_ADDR"];

    if ($ip=="210.245.120.84"){
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://yahoo.com/" ); }
    else {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.vnpower.org/" ); }
?>

Khi đó máy có IP là 210.245.120.84 sẽ được redirect sang http://www.yahoo.com/ còn tất cả IP còn lại sẽ được chuyển sang http://www.vnpower.org/

Tương tự với 2 địa chỉ IP cho trước:

CODE
<?php
$ip=$_SERVER["REMOTE_ADDR"];

    if ($ip=="210.245.120.84") {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.yahoo/" ); }
    elseif ($ip=="230.162.5.184") {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.google.com/" ); }
    else {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: http://www.vnpower.org/" ); }
?>

- IP mới thêm vào sẽ được chuyển sang http://www.google.com/
- Và nhiều hơn 2 IP thì ta cũng làm tương tự với elseif.
Go to the top of the page
 
+Quote Post
blackeye87
post Feb 21 2008, 05:37 PM
Post #4


Yahoo! Playa
Group Icon

Group: Member
Posts: 137
Cash: 16k
Age: N/A
Joined: 14-January 07
Member No.: 20,857
Yahoo! Status: N/A



vậy nếu em để 1 file txt dạng ip.txt và thay vào phần IP có sẵn của anh thì nó có run được ko anh?
em muốn tạo 1 list IP có sẵn !Cảm ơn anh đã chỉ dẫn !
Go to the top of the page
 
+Quote Post
PETER
post Feb 21 2008, 05:41 PM
Post #5


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Lấy một trường hợp khác. Anh chọn 2 địa chỉ IP là: 210.245.120.84 và 230.162.5.18 khi truy cập vào trang web ta muốn máy có IP là 210.245.120.84 sẽ bị chuyển sang yahoo.com, máy có IP là 230.162.5.18 sẽ bị pasword protect chặn lại còn tất cả máy khác thì được chuyển sang vnpower.org. Ta làm như sau:

CODE
<?php
$ip=$_SERVER["REMOTE_ADDR"];

    if ($ip=="210.245.120.84") {
          Header( "HTTP/1.1 301 Moved Permanently" );
          Header( "Location: http://www.yahoo/" ); }
    elseif ($ip=="230.162.5.18") {
          header('HTTP/1.1 401 Unauthorized');
          header('WWW-Authenticate: Basic realm="VNP Community"'); }
    else {
          Header( "HTTP/1.1 301 Moved Permanently" );
          Header( "Location: http://www.vnpower.org/" ); }
?>
Go to the top of the page
 
+Quote Post
blackeye87
post Feb 21 2008, 05:53 PM
Post #6


Yahoo! Playa
Group Icon

Group: Member
Posts: 137
Cash: 16k
Age: N/A
Joined: 14-January 07
Member No.: 20,857
Yahoo! Status: N/A



em hiểu ý anh nói..Nhưng em có khoảng hơn 600 IP cần chuyển..Mình ko thể code để gọi từ đường dẫn của 1 file txt được hả anh?
Go to the top of the page
 
+Quote Post
PETER
post Feb 21 2008, 06:17 PM
Post #7


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Bài kế bên trên không phải là anh reply cho câu hỏi thứ 2 của em mà là bài anh giới thiệu. Về câu hỏi của em thì tất nhiên là được nhưng chờ anh code xong sao cho đơn giản rồi anh hướng dẫn cho. Còn 1 số yêu cầu trợ giúp của ToanNguyen và 1 số member anh phải trợ giúp. Trợ giúp họ xong rồi tới em mới công bằng.
Go to the top of the page
 
+Quote Post
blackeye87
post Feb 21 2008, 08:21 PM
Post #8


Yahoo! Playa
Group Icon

Group: Member
Posts: 137
Cash: 16k
Age: N/A
Joined: 14-January 07
Member No.: 20,857
Yahoo! Status: N/A



Cảm ơn anh rất nhiều.. 4.gif
Go to the top of the page
 
+Quote Post
PETER
post Mar 8 2008, 09:44 PM
Post #9


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Mốt số mẫu 301 Redirect đã có trên VNP Web Tool (301 Redirect)
- Lưu ý: Ở các mẫu bên dưới các bạn thay http://www.new-url.com bằng url mà các bạn muốn redirect tới.

ColdFusion Redirect:

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-url.com">



PHP Redirect:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>


ASP Redirect:

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com/");
%>



ASP .NET Redirect:

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>



JSP (Java) Redirect:

<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>



CGI PERL Redirect:

$q = new CGI;
print $q->redirect("http://www.new-url.com/");



Ruby on Rails Redirect:

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end



Htaccess Redirect:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]




HTML Metatag Redirect: (Not 301 Redirect)

<meta http-equiv="refresh" content="x; URL=http://www.new-url.com">
- x là số thời gian bạn muốn trang web chuẩn bị tự động di chuyển tính bằng giây (sec).


Java-Script Redirect: (Not 301 Redirect)

<script>window.location.replace('http://www.new-url.com');</script>
Go to the top of the page
 
+Quote Post
PETER
post Mar 8 2008, 09:55 PM
Post #10


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Một số mẫu Redirect domain.com sang www.domain.com.
Demo: vnpower.org

1. Sử dụng file .htaccess: (301 Redirect)
- Các bạn tạo file .htaccess với nội dung: (Nếu trên host đã có file .htaccess rồi thì các bạn chỉ cần add thêm vào).

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

+ Thay domain.com bằng tên domain của bạn.


2. Sử dụng PHP: (301 Redirect)
- File .htaccess có tác dụng với tất cả những trường hợp file nằm trong folder chứa file cùng cấp và thứ cấp. Đôi khi bạn chỉ muốn dùng cho 1 file hay trường hợp nào đó thôi chứ không muốn dùng cho tất cả. Nếu đang sử dụng PHP các bạn làm như sau:
+ Thêm mã PHP dưới đây vào file PHP mà bạn đang sử dụng.

//<?php
$host = $HTTP_SERVER_VARS['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];

if (substr($host,0,4) != "www.") {
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.domain$self" );
}
//?>

+ Thay domain.com bằng domain của bạn.
Go to the top of the page
 
+Quote Post
jojobemfit
post Jul 18 2008, 01:18 AM
Post #11


Junior Member
Group Icon

Group: Member
Posts: 2
Cash: 2k
Age: N/A
Joined: 18-July 08
Member No.: 112,023
Yahoo! Status: N/A



Anh PETER giúp em cái này với, theo em biết thì bọn aff nó sẽ theo dõi link và biết mình đặt link của nó ở đâu
Vậy now em xin hỏi, site của em là free1000address.com và cái thứ 2 là free1000address.com/traffic .Em đã có khoảng 10 cái link và auto click ở free1000address.com/click
Có cách nào để bọn redirect tất cả các link ở free1000address.com/traffic để nó trở về free1000address.com không ạ
Như thế có nghĩa là mình click vào link ở free1000address.com/traffic thì aff nó lại hiểu là traffic đến từ site chính của em
HÌnh như là phải làm 1 file redirect thì phải
Mong anh giúp
Go to the top of the page
 
+Quote Post
PETER
post Jul 18 2008, 10:15 AM
Post #12


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Anh chưa hiểu trường hợp của em lắm, free1000address.com/click và free1000address.com/traffic có liên quan gì nhau?
Go to the top of the page
 
+Quote Post
y5cafe
post Jul 18 2008, 10:48 AM
Post #13


Junior Member
Group Icon

Group: Member
Posts: 4
Cash: 4k
Age: N/A
Joined: 18-July 08
Member No.: 112,044
Yahoo! Status: N/A



Bác PETER cho em hỏi.

Site em trước đây có đường dẫn là http://www.y5cafe.info

Nhưng bây giờ do một số lý do em phải chuyển nó vào trong một thư mục "mc"

Có nghĩa là những đường do search engine hoặc người dùng lưu lại sẽ dẫn đến lỗi 404.

ví dụ:
http://www.y5cafe.info/thitruong/tong-hop-...e-ngay-18-07-08

Sẽ bị lõi vì ko có thư mục "mc"

Vậy bác có cách nào giúp em viết lại URL để có thêm thư mục "mc".

Thanks pác.

Links and References to Other Pages:

http://www.y5cafe.info
http://www.y5cafe.info/thitruo...uong-ca-phe-ngay-18-07-08
Go to the top of the page
 
+Quote Post
PETER
post Jul 18 2008, 11:08 AM
Post #14


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Cái này PETER nhớ là có hướng dẫn rồi, và cũng dùng nhiều rồi. Trước đây khi yahoo-69.com chuyển qua vnpower.org thì PETER cũng làm như vậy để áp dụng cho kết quả tìm kiếm trên các search engine. Cái này có nhiều cách để làm tùy vào múc đích bổ sung của mỗi webmaster.

Nếu chỉ muốn redirect dạng đó thì chỉ cần làm 1 file index.php ở thực gốc với nội dung đơn giản sau:

CODE
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.y5cafe.info/mc$_SERVER[REQUEST_URI]" );
?>

Đồng thời bạn phải giữ lại phần rewrite url trong file .htaccess của thư mục gốc.
Lưu ý: Tùy vào file gốc được rewrite URL mà đổi tên file index.php thành tên file phù hợp.

Ngoài ra còn rất nhiều cách tùy theo mục đích bổ sung của webmaster như check referer, giữ lại trang index với nội dung khác hoặc chỉ redirect với các search engine ngoài ra dùng file .htaccess cho nhanh cũng được.

Mà hình như bạn tính "thử" PETER thì phải.
Go to the top of the page
 
+Quote Post
jojobemfit
post Jul 18 2008, 01:30 PM
Post #15


Junior Member
Group Icon

Group: Member
Posts: 2
Cash: 2k
Age: N/A
Joined: 18-July 08
Member No.: 112,023
Yahoo! Status: N/A



QUOTE (PETER @ Jul 18 2008, 10:15 AM) *
Anh chưa hiểu trường hợp của em lắm, free1000address.com/click và free1000address.com/traffic có liên quan gì nhau?

free1000address.com/traffic : no se auto click cho những link affiliate nằm bên trong site free1000address.com
Có cách nào để autoclick ink affiliate ở trong free1000address.com/traffic thì affiliate nó hiểu là click đó đến từ free1000address.com mà khi click xong no' vẫn đến đước cái affiliate kia ?
Go to the top of the page
 
+Quote Post
PETER
post Jul 18 2008, 05:29 PM
Post #16


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Về nguyên tắc click ở đâu thì referer được tính tại đó. Tuy nhiên không phải là tuyệt đối, còn phải xem qua nguyên tắc "auto click" của bạn dựa trên mã nguồn thực hiện chức năng đó.
Go to the top of the page
 
+Quote Post
paint
post Jul 18 2008, 06:27 PM
Post #17


Junior Member
Group Icon

Group: Member
Posts: 8
Cash: 3k
Age: N/A
Joined: 21-July 06
Member No.: 15,864
Yahoo! Status: N/A



Em có một trường hợp redirect nhờ anh PETER giúp, trong trang web của em có các link khác do member gửi lên, em muốn thay link đó theo dạng http://domain_cua_em.com/redirect.php?u={base64} với {base64} là đoạn link của member gửi lên đã được mã hóa sang base64 (base64_encode).

Bây giờ cái quan trọng là anh PETER code giúp em file redirect.php để nó tự đồng reditect sang các link tương ứng đã được mã hóa sang base64.

Thanks anh 1.gif
Go to the top of the page
 
+Quote Post
PETER
post Jul 18 2008, 06:49 PM
Post #18


Ψ Yahoo-69 Power Ψ
Group Icon

Group: Owner
Posts: 2,708
Cash: 68,497k
Age: 24
Joined: 11-June 07
From: Saigon
Member No.: 1
Yahoo! Status:



Theo ý của em thì file redirect.php sẽ đặt ở thư mục gốc nên có link dạng: http://domain.com/redirect.php

Ok, em làm 1 file redirect.php với nội dung sau:

CODE
<?

$peter=$_GET["u"];
$peter=base64_decode($peter);

if ($peter) {
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: $peter" );
    die();
}

else {
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: /" );
    die();
}

?>

Cái này anh có làm rồi 3.gif nhưng anh code hới khác 1 tí.
Demo: http://www.vnpower.org/Lien-Ket
Go to the top of the page
 
+Quote Post
mai04
post Jul 19 2008, 07:37 PM
Post #19


Junior Member
Group Icon

Group: Member
Posts: 29
Cash: 2k
Age: N/A
Joined: 11-December 06
Member No.: 19,940
Yahoo! Status: N/A



Very thanks 8.gif
Go to the top of the page
 
+Quote Post
y5cafe
post Jul 20 2008, 07:40 AM
Post #20


Junior Member
Group Icon

Group: Member
Posts: 4
Cash: 4k
Age: N/A
Joined: 18-July 08
Member No.: 112,044
Yahoo! Status: N/A



QUOTE (PETER @ Jul 18 2008, 11:08 AM) *
Cái này PETER nhớ là có hướng dẫn rồi, và cũng dùng nhiều rồi. Trước đây khi yahoo-69.com chuyển qua vnpower.org thì PETER cũng làm như vậy để áp dụng cho kết quả tìm kiếm trên các search engine. Cái này có nhiều cách để làm tùy vào múc đích bổ sung của mỗi webmaster.

Nếu chỉ muốn redirect dạng đó thì chỉ cần làm 1 file index.php ở thực gốc với nội dung đơn giản sau:

CODE
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.y5cafe.info/mc$_SERVER[REQUEST_URI]" );
?>

Đồng thời bạn phải giữ lại phần rewrite url trong file .htaccess của thư mục gốc.
Lưu ý: Tùy vào file gốc được rewrite URL mà đổi tên file index.php thành tên file phù hợp.

Ngoài ra còn rất nhiều cách tùy theo mục đích bổ sung của webmaster như check referer, giữ lại trang index với nội dung khác hoặc chỉ redirect với các search engine ngoài ra dùng file .htaccess cho nhanh cũng được.

Mà hình như bạn tính "thử" PETER thì phải.


Cám ơn bác PETER nhiều, mình đã làm được nhưng ...
Nếu CMod của thư mục web là 777 thì nó mới tự động thêm "mc" vào, còn change cmod sang 755 thì lại không được,

Pác PETER có cách nào khắc phục giúp em không.

Thanks pác nhiều.
Go to the top of the page
 
+Quote Post