メモ > サーバ > 各論: エトセトラ > Apacheでリクエストを操作する
Apacheでリクエストを操作する
http://www.example.com/school/ 内へのアクセスを。
https://example.jp/ に転送(リダイレクト)する例。(階層を上げて、SSLも強制。)
school フォルダ直下に .htaccess を作成して設定するものとする。
ロリポップで設定する場合、「Lolipop.txt」の「HTTPからHTTPSにリダイレクトする」を参照。
また特殊文字のエンコードを防ぐため、必要に応じて「[R=301,L]」は「[R=301,L,NE]」にするといい。
日本語URLを含むリダイレクトの罠にかかっても無事でいられる2つの対策
https://alaki.co.jp/blog/?p=2394
■一括指定なら以下
RewriteEngine On
RewriteRule ^(.*)$ https://example.jp/$1 [R=301,L]
■個別にURLを並べるなら以下
RewriteEngine On
RewriteRule ^$ https://example.jp/ [R=301]
RewriteRule ^about/$ https://example.jp/about/ [R=301]
RewriteRule ^education/$ https://example.jp/educatio/ [R=301]
RewriteRule ^education/kyoto/$ https://example.jp/educatio/kyoto [R=301]
RewriteRule ^education/tokyo/$ https://example.jp/education/tokyo [R=301]
RewriteRule ^education/fukuoka/$ https://example.jp/education/fukuoka [R=301]
RewriteRule ^summary/$ https://example.jp/summary [R=301]
RewriteRule ^summary/course001/$ https://example.jp/summary/elementary_training [R=301]
RewriteRule ^summary/course003/$ https://example.jp/summary/secondary_specialty [R=301]
RewriteRule ^summary/course004/$ https://example.jp/summary/advanced_encouragement [R=301]
リダイレクトの後の処理が実行されてしまう場合、[R=301] の部分を [R=301,L] に変更する。
特に理由がなければ [R=301,L] としておく方がいいかもしれない。
■トップページにアクセスしたら /about/ にリダイレクトする例
RewriteEngine On
RewriteRule ^$ /about/ [R=301,L]
■メモ
正規表現が不要な単純なリダイレクトなら、
RewriteRule ではなく Redirect permanent を使うべきか。
要検証。
いま復習しておきたい、301リダイレクトの設定あれこれ。|ビリオンプランのスタッフブログ
http://www.billionplan.com/blog/seo/redirect-htaccess-301.html