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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: วิธีการ Redirect จาก http เป็น https ด้วย .htaccess สำหรับ Server ที่ใช้ Apache

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

โดย superhero » 30/08/2019 11:33 am

ถ้าเป็นผู้ดูแลเว็บก็แก้ใน conf.d เลยครับ
<VirtualHost *:80>
ServerName hostname.com
ServerAlias hostname.com
Redirect / https://hostname.com
</VirtualHost>

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

โดย mindphp » 27/08/2019 12:10 pm

สำหรับใครใช้ 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>
แทนของเดิม

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

โดย mindphp » 27/08/2019 12:08 pm

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

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

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

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

โดย mindphp » 02/12/2018 9:16 pm

จากบทความก่อน เป็นการสร้าง 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 ที่สมบูรณ์

ข้างบน