Hướng dẫn apache rewrite index php - apache rewrite index php

Rewrite URL với rewrite_module

RewriteEngine on
4 là một module mở rộng của Server Apache HTTP, nếu có nạp và sử dụng module này nó cho phép bạn viết lại Url. Chức năng chính của nó là ánh xạ một địa chỉ URL đến một vị trí file cụ thể của Server, tuy nhiên bạn có thể làm nhiều hơn thế như chuyển hướng, chặn truy cập ...
RewriteEngine on
4 phân tích yêu cầu gửi đến bằng cách sử dụng biểu thức chính quy (Xem : Biểu thức chính quy)

RewriteEngine on
4 mạnh và mềm dẻo, bạn có thể đưa ra số quy tắc không hạn chế để biến đổi URL, mỗi quy tắc đó cũng hoạt động dựa vào số điều kiện bạn đặt ra một cách không hạn chế.

Sử dụng mod_rewrite ở đâu ? : Nó cung cấp các chỉ thị để bạn sử dụng như

RewriteEngine on
7,
RewriteEngine on
8,
RewriteEngine on
9 ... để bạn viết nó trong
RewriteBase URL-path
0 hoặc trong file
RewriteBase URL-path
1
: Nó cung cấp các chỉ thị để bạn sử dụng như
RewriteEngine on
7,
RewriteEngine on
8,
RewriteEngine on
9 ... để bạn viết nó trong
RewriteBase URL-path
0 hoặc trong file
RewriteBase URL-path
1

  • Nếu chọn viết trong
    RewriteBase URL-path
    1, thì các Rewrite có hiệu lực trên thư mục đặt file đó. Khi Apache truy cập đến bất kỳ thư mục con nào thì nó cũng tìm xem trong thư mục đó có file
    RewriteBase URL-path
    1 không để nạp vào. Viết cách này khá linh hoạt, nhưng đôi khi làm chậm một chút, nếu có thể ở cấu hình
    RewriteBase URL-path
    4 tắt hẳn
    RewriteBase URL-path
    1 bằng chỉ thị
    RewriteBase URL-path
    6 để chỉ viết trong cấu hình Apache
    RewriteBase URL-path
    0
  • Các quy tắc ReWrite viết trong
    RewriteBase URL-path
    0 ở các
    RewriteBase URL-path
    9 của
    RewriteBase URL-path
    4, kết quả thì cũng tương tự viết trong
    RewriteBase URL-path
    1, nó nhanh hơn vì các luật ReWrite Apache không phải tìm vào nạp mỗi khi truy cập, viết xong phải khởi động lại Apache để có hiệu lực

Nhớ là để sử dụng cần đảm bảo Apache có nạp

RewriteEngine Pattern Substitution [flags]
2

LoadModule rewrite_module modules/mod_rewrite.so

Các chỉ thị rewrite_module

Module rewrite_module cung cấp thêm cho Apache các chỉ thị để dùng gồm:

RewriteEngine Pattern Substitution [flags]
3
RewriteEngine on
9
RewriteEngine on
8
RewriteEngine Pattern Substitution [flags]
6
RewriteEngine on
7
RewriteEngine Pattern Substitution [flags]
8

RewriteEngine Pattern Substitution [flags]
3
Bật, tắt Rewrite, nhớ là Rewrite mặc định không có hoạt động cho tất cả thư mục, ở thư mục nào muốn kích hoạt thì bật nên. Tóm ở mỗi thư mục bắt đầu viết các rewrite cần có dòng
RewriteEngine on
RewriteRule ^welcome\.html$ /html/index.html [L]
0
RewriteBase URL-path
Thiết lập URL cơ sở cho thư mục
RewriteEngine on
9
RewriteEngine Pattern Substitution [flags]

Chỉ thị này sẽ tiến hành thay thế URL theo cú pháp ở trên, các chuỗi URL gửi đến mà phù hợp với mẫu

RewriteRule ^welcome\.html$ /html/index.html [L]
2 (mẫu là biểu thức chính quy) thì nó thay thể URL gửi đến thành URL mới là
RewriteRule ^welcome\.html$ /html/index.html [L]
3, phía sau cú pháp có thể có một số loại cờ thiết lập

Ví dụ khi URL gửi đến là

RewriteRule ^welcome\.html$ /html/index.html [L]
4 thì thực tế Apache sẽ truy cập
RewriteRule ^welcome\.html$ /html/index.html [L]
5 trên đĩa

RewriteRule ^welcome\.html$ /html/index.html [L]

Trong đó:

  • RewriteRule ^welcome\.html$ /html/index.html [L]
    6 là mẫu kiểm tra URL, các ký hiệu
    RewriteRule ^welcome\.html$ /html/index.html [L]
    7
    RewriteRule ^welcome\.html$ /html/index.html [L]
    8
    RewriteRule ^welcome\.html$ /html/index.html [L]
    9 ... xem tại biểu thức chính quy

    Trong biểu thức

    RewriteCond TestString CondPattern [flags]
    0 của mẫu, các nhóm
    RewriteCond TestString CondPattern [flags]
    1 theo thứ tự từ trái sang phải là nhóm 1, nhóm 2 ... nó có thể được gọi ra từ
    RewriteRule ^welcome\.html$ /html/index.html [L]
    3 hoặc từ
    RewriteEngine on
    8. Nên rất cần hiểu về biểu thức chính quy.
    Hướng dẫn apache rewrite index php - apache rewrite index php

    Mẫu là biểu thức chính quy

    RewriteCond TestString CondPattern [flags]
    4 thì có hai nhóm, nhóm 1 là
    RewriteCond TestString CondPattern [flags]
    5, nhóm 2 là
    RewriteCond TestString CondPattern [flags]
    6 thì trong
    RewriteRule ^welcome\.html$ /html/index.html [L]
    3,
    RewriteEngine on
    8 lấy ra chuỗi theo nhóm bằng ký hiệu
    RewriteCond TestString CondPattern [flags]
    9 là
    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    0,
    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    1

  • RewriteRule ^welcome\.html$ /html/index.html [L]
    5 chuỗi thay thế (
    RewriteRule ^welcome\.html$ /html/index.html [L]
    3) nếu URL gửi đến phù hợp với mẫu. Nếu
    RewriteRule ^welcome\.html$ /html/index.html [L]
    3 bằng
    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    5 có nghĩa là URL sẽ không bị biến đổi (thường dùng truy cập các thư mục file).
  • RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    6 là cờ thiết lập cho biết kếu luật này thi hành thì không thi hành các luật ở
    RewriteEngine on
    9 viết sau nó nữa. Ngoài ra có các cờ thiết lập khác cho thêm vào
    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    8, nhiều cờ thì cách nhau dấu
    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)
    9 như:
    • RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      0 trả về mã
      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      1 cấm truy cập
    • RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      2 trả về mã
      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      3 file bị xóa
    • RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      4 Rule lặp đị lặp lại cho đến khi kết quả URL không còn bị thay thế. Có thể chỉ ra số lần lặp
      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      5
    • RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      6 chuyển hướng URL với mã
      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      7 chỉ ra (ví dụ
      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [L]
      
      ...Các RewriteRule khác
      
      8)
RewriteEngine on
8
RewriteCond TestString CondPattern [flags]

Viết điều kiện để

RewriteEngine on
9 phía dưới nó được ứng dụng. Trường hợp đơn giản đầu tiên URI kiểm tra bởi
RewriteEngine on
8 (có thể kiểm tra URL, Method, biến môi trường ...), nếu điều kiện đúng thì
RewriteEngine on
9 gần nhất sau
RewriteEngine on
9 sẽ được dùng để Rewrite.
RewriteEngine on
8 có kiểm tra điều kiện rất đa dạng, dưới đây là một số thứ có thể tham khảo:

RewriteEngine On

#Chuyển hướng 1 URL cụ thể
RewriteEngine  on
RewriteRule    "^/url-cu\.html$"  "url-moi.html"  [R=301]

#Chuyển hướng thư mục news sang domain (server mới)
RewriteRule   "^/docs/(.+)"  "http://new.example.com/docs/$1"  [R,L]
5 cờ thiết lập, ví dụ
RewriteEngine On

#Chuyển hướng 1 URL cụ thể
RewriteEngine  on
RewriteRule    "^/url-cu\.html$"  "url-moi.html"  [R=301]

#Chuyển hướng thư mục news sang domain (server mới)
RewriteRule   "^/docs/(.+)"  "http://new.example.com/docs/$1"  [R,L]
6 toán tử hoặc kết hợp hai
RewriteEngine on
8 (mặc định là toán từ và.
RewriteEngine On

#Chuyển hướng 1 URL cụ thể
RewriteEngine  on
RewriteRule    "^/url-cu\.html$"  "url-moi.html"  [R=301]

#Chuyển hướng thư mục news sang domain (server mới)
RewriteRule   "^/docs/(.+)"  "http://new.example.com/docs/$1"  [R,L]
8 phân biệt chữ hoa, thường

RewriteEngine On

#Chuyển hướng 1 URL cụ thể
RewriteEngine  on
RewriteRule    "^/url-cu\.html$"  "url-moi.html"  [R=301]

#Chuyển hướng thư mục news sang domain (server mới)
RewriteRule   "^/docs/(.+)"  "http://new.example.com/docs/$1"  [R,L]
9 mẫu - biểu thức chính quy để kiểm tra sự phụ hợp của
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
0, hoặc một số giá trị đặc biệt như:

  • RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    1 kiểm tra TestString là file trên đĩa (có filesize, name)
  • RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    2 kiểm tra là symbolic link
  • RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    3 kiểm tra là thư mục trên đĩa

RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
0 chuỗi chứa tham số kiểm tra:

  • Chứa biến toàn cục bằng cú pháp
    RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    5, các biến gửi đến như đã biết trong PHP như:
    RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    6,
    RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    7 (IP),
    RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    8,
    RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
    RewriteCond "%{HTTP_HOST}" "!^$"
    RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]
    
    9,
    RewriteEngine on
    00,
    RewriteEngine on
    01,
    RewriteEngine on
    02,
    RewriteEngine on
    03. Tương tự biến môi trường đưa vào bằng
    RewriteEngine on
    04, các Header HTTP đư vào bằng
    RewriteEngine on
    05

    Ví dụ Rewrite khi trình iPhone hoặc Android truy cập (kiểm tra USER_AGENT)

    RewriteCond  "%{HTTP_USER_AGENT}"  "(iPhone|Android)"
    RewriteRule  ... (viết rule)

Truy cập đến thư mục

Bạn viết Rule đơn giản đầu tiên: Khi truy vấn URI phù hợp với đường dẫn đến file trên đĩa thì có một Rule không thay đổi URI áp dụngKhi truy vấn URI phù hợp với đường dẫn đến file trên đĩa thì có một Rule không thay đổi URI áp dụng

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

...Các RewriteRule khác

Ví dụ trên mục đích là các truy vấn đến file cụ thể trên đĩa thì không làm gì (RewriteRule có [L], nên nếu là file thật thì quy tắc Rewrite sẽ kết thúc ở đây), nếu không phải là file thì sẽ chuyển sang các RewriteRule

Chuyển hướng URL

Chuyển hướng - Redirect rất hay áp dụng, như

  • Xóa (đổi địa chỉ) một URL trên website, nhưng muốn khi người dùng truy cập vào sẽ tự chuyển sang một trang khác
  • Chuyển sang một domain mới
RewriteEngine On

#Chuyển hướng 1 URL cụ thể
RewriteEngine  on
RewriteRule    "^/url-cu\.html$"  "url-moi.html"  [R=301]

#Chuyển hướng thư mục news sang domain (server mới)
RewriteRule   "^/docs/(.+)"  "http://new.example.com/docs/$1"  [R,L]

Mọi URI không có

RewriteEngine on
06 (mydomain.com...) chuyển sang có
RewriteEngine on
06 (www.mydomain.com...)

RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]

Mọi URI có www sẽ chuyển sang không có www

RewriteEngine on
0

Tránh mất băng thông

Giả sử trên host của bạn

RewriteEngine on
08, có một hình ảnh. Nếu một trang nào đó lấy URL ảnh và hiện thị ở trang của họ thì không được phép

RewriteEngine on
1

Mọi truy vấn khác đều chạy file index.php

Đây là cách PHP hay áp dụng nhất là trong các Framework

RewriteEngine on
2

Ví dụ trong ZendFramework

RewriteEngine on
3

Ngoài

RewriteEngine on
09 Apache còn có
RewriteEngine on
10, cung cấp chỉ thị
RewriteEngine on
11,
RewriteEngine on
12 thay thế cho nhiều trường hợp của
RewriteEngine on
09, đơn giản hơn hiệu quả hơn. Nên nếu có thể thì áp dụng module này để nâng cao hiệu suất