ショウジンブログ on Hatena

“お芝居をしないと、この社会では異邦人として扱われるほかない”

AWS EC2でURLの正規化@Amazon EC2

.htaccessを使ってURLの正規化を施します。

デフォルトのままだとAWS EC2のApacheでは.htaccessが使えないので注意。

Apacheの設定ファイルhttpd.confを編集する必要があります。

参考

blog.showzine.co

URLの正規化

wwwなしで統一し、index.html/index.phpを/にリダイレクト

showzine.coの例

<Files ~ "^\.(htaccess|htpasswd)$">

deny from all
</Files>
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(www\.showzine\.co)(:80)? [NC]
RewriteRule ^(.*) http://showzine.co/$1 [R=301,L]
order deny,allow

# index.html, index.phpは/にリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.(html|php)
RewriteRule ^(.*)index.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]

wwwのありとなしを統一

.htaccessをルートに用意してその中に以下のように記述。

# wwwありで統一の例

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.hoge
RewriteRule ^(.*)$ http://www.example.hoge/$1 [R=301,L]

# wwwなしで統一の例

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.hoge
RewriteRule (.*) http://example.hoge/$1 [R=301,L]

index.html, index.phpを/にリダイレクト

RewriteEngine on
Options +FollowSymLinks

# index.html, index.phpは/にリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.(html|php)
RewriteRule ^(.*)index.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]