
วิธีแก้ปัญหา Duplicate Content ด้วย .htaccess (ลบ www และ index.php)
Remove www and index.php from URL
การ Canonical URL ด้วย .htaccess
ทำไมต้อง Canonical URL?
Canonical URL คือการทำให้ทุกลิงก์ของเว็บไซต์เป็นมาตรฐานเดียวกัน เช่น ไม่ให้มีทั้ง http://example.com/
และ http://www.example.com/
หรือ example.com/index.php
เพราะอาจทำให้เกิด Duplicate Content และกระทบต่อ SEO ได้
ตัวอย่าง .htaccess สำหรับ Canonical URL
# ลบ index.php และ index.html
RewriteCond %{REQUEST_URI} !/$ [NC]
RewriteCond %{REQUEST_URI} /index.(html?|php)$ [NC]
RewriteRule .* https://example.com/ [R=301,L]
# ลบ www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule .* https://example.com/$1 [R=301,L]
อธิบายการทำงาน
- ส่วนแรก: Redirect URL ที่ลงท้ายด้วย
index.php
,index.html
ไปยังหน้าแรก - ส่วนที่สอง: Redirect URL ที่มี
www.
ไปยังโดเมนหลักแบบไม่มี www [R=301]
= Permanent Redirect (เหมาะกับ SEO)[L]
= หยุดอ่านกฎอื่น ๆ ต่อ
สิ่งที่ควรทำ
- เปลี่ยน
example.com
เป็นโดเมนจริงของคุณ - ถ้าเว็บไซต์ใช้ HTTPS ให้แก้เป็น
https://example.com/
- สำรองไฟล์ .htaccess เดิมก่อนแก้ไข
- ทดสอบการทำงานหลังอัปโหลด
ประโยชน์ที่ได้
- ป้องกัน Duplicate Content
- ช่วยเรื่อง SEO เพราะ Google จะ index เพียง URL เดียว
- ทำให้ URL สวยงามและสะอาดขึ้น
- เพิ่มความน่าเชื่อถือของเว็บไซต์
Here’s a handy .htaccess snippet for canonicalizing URLs by removing the “www”, “index.php”, and “index.html” from all requests.
To make it happen, backup your current .htaccess file and then add these rules:
# remove index
RewriteCond %{REQUEST_URI} !/$ [NC]
RewriteCond %{REQUEST_URI} /index.(html?|php)$ [NC]
RewriteRule .* http://example.com/ [R=301,L]
# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule .* http://example.com/$1 [R=301,L]
Then edit each instance of example.com with your own domain and upload to your server. To verify that it’s working, try requesting some pages while including the “www”, “index.php”, and “index.html” in the URLs.
ที่มาของบทความ
หากต้องการข้อมูลเพิ่มเติมสามารถเข้าไปดูที่ลิ้งค์นี้ได้เลยครับ
https://wp-mix.com/remove-www-index-htaccess/