Mắt Bão Team Blog
Storming the future

URL Rewriting dùng ASP/IIS

December 1, 2009 16:12 by cube
URL Rewrite giúp địa chỉ trang web dễ nhìn hơn, đặc biệt là giúp SEO (Search Engine Optimation) tốt hơn. Hôm nay tôi xin hướng dẫn bạn làm URL Rewrite trên ASP/IIS.

Tôi có : http://degiocuondi.com/news.asp?id=1
nhưng tôi muốn bạn nhìn thấy dạng : http://degiocuondi.com/news/1

Sự khác nhau là “/news/1” dể coi hơn “/news.asp?id=1”.

Chúng ta cùng bắt đầu:

 

Bước 1: Tạo file asp-rewrite.asp trên website http://degiocuondi.com/ với code:


1.  Class URL

2.   

3.   Private aURL ' Mảng các phần tử trong URL

4.   

5.   Private Sub Class_Initialize()

6.    sTemp = Request.Querystring

7.    ' Bỏ các ký tự dầu trong URL: “404;”, “403”

8.    sTemp = Right(sTemp, len(sTemp) - 4)

9.    ' Bỏ đi phần địa chỉ chính trong URL

10.  sTemp = replace(sTemp, "http:// degiocuondi.com:80/", "")

11.  sTemp = replace(sTemp, "http:// degiocuondi.com /", "")

12.  ' Thêm ký tự “/” sau cùng nếu chưa có

13.  If Right(sTemp, 1) <> "/" then

14.   sTemp = sTemp & "/"

15.  End If

16.  aURL = split(sTemp, "/")

17. End Sub

18. 

19. Public Property Get url(index)

20.  If index > uBound(aUrl) then

21.   url = ""

22.  Else

23.   url = trim(aUrl(index))

24.  End If

25. End Property

26. 

27.End Class

28. 

29.Set MyUrl = New URL

30.Session(0) = MyUrl.url(1))

31.Server.Transfer("/" & MyUrl.url(0) & ".asp”)

Bạn để ý, mấu chốt nằm ở lệnh:

 

server.transfer

Lệnh này chuyển xử lý đến 1 file nào đó (news.asp chẳng hạn)

 

Ý nghĩa của trang asp-rewrite.asp là khi người dùng truy cập địa chỉ

 http://degiocuondi.com/news/1

sẽ nhận được kết quả

 

 

File asp-rewrite.asp có nhiệm vụ transfer xử lý về

http://degiocuondi.com/news.asp?id=1

 
Bước 2: Tạo file news.asp trên website http://degiocuondi.com/ với code:


1.  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

2.  <%

3.  id = Session(0)

4.  Response.Write "Nội dung tin số " & id

5.  %> 

Bước 3: Cấu hình để chuyển lỗi 404, 403, 405 về file asp-rewrite.asp xử lý. Lỗi 500 chuyển xử lý về global.asa

 

  • 403;14 (Forbidden - Directory Listing Denied) ==> asp-rewrite.asp
  • 404 (Not Found) ==> asp-rewrite.asp
  • 405 (Method Not Allowed) ==> asp-rewrite.asp
  • 500;15 (Internal Server Error) ==> global.asa (để hiện thị lỗi)

Hoàn tất! Bây giờ bạn có thể vào http://degiocuondi.com/news.asp?id=1001 mà cũng có thể vào http://degiocuondi.com/news/1001

Cảm ơn sự trợ giúp của Rust Indy/ASP Free.Com !


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
 
Những gì chúng tôi đăng ở trang này nhằm mục đích tham khảo.
Chúng tôi không chịu trách nhiệm về sự chính xác và việc sử dụng của người xem. Những thông tin đăng lại hoặc trích đăng được giữ bản quyền của nguồn tin.
(This posting is provided "AS IS" with no warranties, and confers no rights. Information are copyright of their owners.)