COLO.in.th > คลังความรู้ > Website > วิธีการ redirect เว็บไซด์ด้วย .htaccess


วิธีการ redirect เว็บไซด์ด้วย .htaccess




RewriteEngine On #เปิดการใช้งาน mod rewrite
RewriteCond %{SERVER_PORT} 80  #ใช้เช็ค พอร์ต
RewriteCond %{HTTP_HOST} !^www\.colo\.in\.th$ [NC] #ใช้เช็ค dns

RewriteCond %{REQUEST_URI} somefolder #ใช้เช็ค path ที่เข้ามา
RewriteRule ^(.*)$ http://www.colo.in.th/$1 [L,R=301] #ใช้สำหรับการคืนค่า

*** หมายเหตุ *** ถ้ามีมากกว่าหนึ่ง พอร์ต ใช้ [OR] เพื่อเชื่อม
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} !^www\.colo\.in\th$ [NC]
RewriteRule ^(.*)$ https://www.colo.in.th/$1 [L,R=301]

//—————————————————————————————-

มาจาก http://support.howhost.com/entries/271311-redirect-htaccess

ตัวอย่างต้นฉบับ

เอกสารนี้จะช่วยอธิบายวิธีการสร้างไฟล์ .htaccess เพื่อทำการ redirect หน้าเว็บไซด์ของคุณ

1. สร้าง Textfile เปล่าด้วย Text Editor เช่น Notepad และทำการ Save as เป็นชื่อ htaccess.txt

2. ทำการแก้ไขไฟล์ htaccess.txt

Redirect ทั้งหน้าเว็บเพ็จไปยัง URL อื่น

Redirect 301 / http://support.colo.in.th/

Redirect index.html ไปยัง Sub Folder ที่ต้องการ

Redirect /index.html http://colo.in.th/newdirectory/

Redirect ไฟล์เก่า ไปยังไฟล์ใหม่

Redirect /olddirectory/oldfile.htmlhttp://colo.in.th/newdirectory/newfile.html

Redirect ไปยังหน้า Index ที่ต้องการ

DirectoryIndex index.html

Redirect User ให้เข้าเว็บไซด์โดยไม่ให้มี www นำหน้า

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.colo\.in\.th$ [NC]
RewriteRule ^(.*)$ http://colo.in.th/$1 [L,R=301]

Redirect User ให้เข้าเว็บไซด์โดยบังคับให้มี www นำหน้า

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.colo\.in\.th$ [NC]
RewriteRule ^(.*)$ http://www.colo.in.th/$1 [L,R=301]

Redirect User เพิ่อให้บังคับใช้ SSL

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.colo.in.th/$1 [R,L]

Redirect User สำหรับใช้ www ที่เป็น http:// และ https://

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.colo\.in.\th$ [NC]
RewriteRule ^(.*)$ http://www.colo.in.th/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} !^www\.colo\.in.\th$ [NC]
RewriteRule ^(.*)$ https://www.colo.in.th/$1 [L,R=301]

Redirect User เพื่ิอบังคับใช้ https:// ในบาง Folder

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} somefolder
RewriteRule ^(.*)$ https://www.colo.in.th/somefolder/$1 [R,L]

3. ทำการ Upload File ไปยัง server ของคุณโดยให้ทำการ Rename ชื่อไฟล์ไปเป็น .htaccess



ขอบคุณ http://9yai.wordpress.com
https://support.hostatom.com/knowledgebase.php?action=displayarticle&id=190



บทความนี้มีประโยชน์กับคุณหรือไม่ ?

บันทึกสู่รายการโปรดของฉัน บันทึกสู่รายการโปรดของฉัน    พิมพ์หัวข้อนี้ พิมพ์หัวข้อนี้

Also Read
แก้ปัญหา IP ใน log apache ไม่ตรง (ดูรายละเอียด: 1776)
How to enable .htaccess for Apache HTTP server (ดูรายละเอียด: 3692)

Powered by WHMCompleteSolution

ภาษา: