วิธีการ Redirect จาก http เป็น https ด้วย .htaccess สำหรับ Server ที่ใช้ Apache

แชร์ความรู้ Linux Ubuntu Web Server บทความ การ config server Linux FreeBSD Apache
การติดตั้ง XAMPP Mysql PHP ใครต้องการแชร์ความรู้เรื่องต่างๆ เหล่านี้ให้ ท่านหรืออื่น โพสที่หมวดนี้ได้

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41230
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

วิธีการ Redirect จาก http เป็น https ด้วย .htaccess สำหรับ Server ที่ใช้ Apache

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

จากบทความก่อน เป็นการสร้าง crs และ กำหนดให้เว็บเราใช้ ssl เพื่อให้เข้าเว็บด้วย protocol https หัวข้อนี้มาดูวิธีกการทำ Redirect จาก http เป็น https ด้วย .htaccess
รูปภาพ
บทความนี้มา สร้างไฟล์ .htaccess ไว้ที่ root ของเว็บ เพิ่มข้อความลงไปในไฟล์ ดังนี้

โค้ด: เลือกทั้งหมด

RewriteEngine on

RewriteCond %{HTTP_HOST} ^mindphp\.com$ [NC]
RewriteRule ^(.*)$ https://www.mindphp.com/$1 [R=301,L]

# บังคับให้ port 80 เปลี่ยนไปเข้าด้วย https:// แทน 
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
ถ้าเว็บเป็น Joomla หรือ phpBB อย่าลืมกำหนดค่าใน admin ของเว็บด้วย เพื่อการใช้งาน https ที่สมบูรณ์
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41230
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: เพิ่มเติมวิธีการ Redirect จาก http เป็น https ด้วย htaccess

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

สำหรับ ของใครใช้ http กับ subdomain ด้วย ให้สร้างไฟล์
.htaccess ไว้ที่ root ของ subdomain ด้วย และ ใส่โค้ดตามนี้

โค้ด: เลือกทั้งหมด

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41230
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: เพิ่มเติมวิธีการ Redirect จาก http เป็น https ด้วย htaccess

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

สำหรับใครใช้ phpBB 3.2 เป็น sub domain
นำโค้ดด้านล่าง .htaccess

โค้ด: เลือกทั้งหมด

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#
# Uncomment the statement below if URL rewriting doesn't
# work properly. If you installed phpBB in a subdirectory
# of your site, properly set the argument for the statement.
# e.g.: if your domain is test.com and you installed phpBB
# in http://www.test.com/phpBB/index.php you have to set
# the statement RewriteBase /phpBB/
#
#RewriteBase /

#
# Uncomment the statement below if you want to make use of
# HTTP authentication and it does not already work.
# This could be required if you are for example using PHP via Apache CGI.
#
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

#
# The following 3 lines will rewrite URLs passed through the front controller
# to not require app.php in the actual URL. In other words, a controller is
# by default accessed at /app.php/my/controller, but can also be accessed at
# /my/controller
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php [QSA,L]

#
# If symbolic links are not already being followed,
# uncomment the line below.
# http://anothersysadmin.wordpress.com/2008/06/10/mod_rewrite-forbidden-403-with-apache-228/
#
#Options +FollowSymLinks
</IfModule>

# With Apache 2.4 the "Order, Deny" syntax has been deprecated and moved from
# module mod_authz_host to a new module called mod_access_compat (which may be
# disabled) and a new "Require" syntax has been introduced to mod_authz_host.
# We could just conditionally provide both versions, but unfortunately Apache
# does not explicitly tell us its version if the module mod_version is not
# available. In this case, we check for the availability of module
# mod_authz_core (which should be on 2.4 or higher only) as a best guess.
<IfModule mod_version.c>
	<IfVersion < 2.4>
		<Files "config.php">
			Order Allow,Deny
			Deny from All
		</Files>
		<Files "common.php">
			Order Allow,Deny
			Deny from All
		</Files>
	</IfVersion>
	<IfVersion >= 2.4>
		<Files "config.php">
			Require all denied
		</Files>
		<Files "common.php">
			Require all denied
		</Files>
	</IfVersion>
</IfModule>
<IfModule !mod_version.c>
	<IfModule !mod_authz_core.c>
		<Files "config.php">
			Order Allow,Deny
			Deny from All
		</Files>
		<Files "common.php">
			Order Allow,Deny
			Deny from All
		</Files>
	</IfModule>
	<IfModule mod_authz_core.c>
		<Files "config.php">
			Require all denied
		</Files>
		<Files "common.php">
			Require all denied
		</Files>
	</IfModule>
</IfModule>
แทนของเดิม
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
superhero
PHP Jr. Member
PHP Jr. Member
โพสต์: 26
ลงทะเบียนเมื่อ: 09/09/2010 11:23 am

Re: วิธีการ Redirect จาก http เป็น https ด้วย .htaccess สำหรับ Server ที่ใช้ Apache

โพสต์ที่ยังไม่ได้อ่าน โดย superhero »

ถ้าเป็นผู้ดูแลเว็บก็แก้ใน conf.d เลยครับ
<VirtualHost *:80>
ServerName hostname.com
ServerAlias hostname.com
Redirect / https://hostname.com
</VirtualHost>
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: facebook.com [Crawler] และบุคลทั่วไป 70