การเขียน mod rewrite เปลี่ยนเส้นทาง domain.com ไปที่ www.domain.com (no-www to www)

ถ้าคุณต้องการบังคับให้ลูกค้าใช้ www.domain.com คุณสามารถเปลี่ยนเส้นทางจาก domain.com ไปเป็นเวอร์ชัน www โดยใช้ไฟล์. htaccess

ในโฟลเดอร์ public_html ของคุณให้สร้างไฟล์ที่ชื่อว่า

.htaccess

และเพิ่มcode

#non-www

# Will automatically redirect requests to www to non-www

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


# Require WWW
# Will automatically redirect requests to non-www to www
# This will exempt robots.txt for search engines
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteCond %{REQUEST_URI} !^/sitemap\.(xml|xml\.gz)$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
</IfModule>

 

บทความจาก  Redirect domain.com to www.domain.com

https://help.directadmin.com/item.php?id=506

 

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

วิธีเขียนโค้ด php ใน ไฟล์นามสกุล html

อยากเขียนโค้ด php ใน ไฟล์นามสกุล html ทำได้โดย .htaccess ความรู้เก่า   <IfModule...

การทำ Redirect from HTTPS เป็น HTTP

There are some specific cases when you want to redirect particular URL or a single website to be...

การทำ Redirect from HTTPS เป็น HTTP

There are some specific cases when you want to redirect particular URL or a single website to be...

จะทำการ block IP โดยใช้ .htaccess ได้อย่างไร

สามารถทำได้โดยระบุ IP ที่ต้องการบลอกที่ .htaccess ไฟล์ โดเยการสร้างไฟล์วางไว้ที่ public_html...

วิธีการเปลี่ยนหน้าแรกจากไฟล์ index เป็นชื่ออื่นที่ต้องการใน hosting ของเรา

ภายในไฟล์ .htacess ให้ระบุข้อมูลต่อไปนี้ครับ DirectoryIndex ชื่อไฟล1 ชื่อไฟล์2 ชื่อไฟล์3...