.htaccessだと動くのにapacheのconfに書くと動かないmod_rewriteリダイレクト

表題の通り、解決したのでメモります。

.htaccessに、http→httpsのリダイレクトをする記述があったので、apacheのconfにまとめようと思いました。

.htaccess

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

/etc/httpd/conf.d/vhosts.conf

<virtualhost *:80="">
DocumentRoot "/home/dacelo/chroot/www/"
ServerName dacelo.space
 
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</virtualhost>

広告

curlで動作確認…

$ curl -I http://dacelo.space/
HTTP/1.1 200 OK

あれっ?

検証した結果、
.htaccessでは「RewiteEngine On」を省略しても動くが、confに書く場合は省略できない。
ということがわかりました。気をつけましょう。

正しい記述

<virtualhost *:80="">
DocumentRoot "/home/dacelo/chroot/www/"
ServerName dacelo.space
 
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</virtualhost>

About: dacelo


Leave a Reply

Your email address will not be published. Required fields are marked *