การเขียน 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

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

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

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

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

Leverage browser caching

Change the request headers of your resources to use caching. Optimize your caching strategy....

How to enable displaying php errors on site

To enable displaying PHP errors Open your website with file manager61 Go...

การป้องกัน Hotlink: วิธีการป้องกันไม่ให้ผู้อื่นขโมยไฟล์ของคุณ

สร้างไฟล์. htaccess ในไดเร็กทอรี public_html ของคุณโดยใช้รหัสต่อไปนี้: RewriteEngine on...