http://www.hogehoge.comにアクセスした際に、モバイルならhttp://www.hogehoge.com/m/に転送させる.htaccessファイルについて記述していきます。
※これらのファイルはサブディレクトリが複数あるとうまく動かないようなので、ドキュメントルートの直下に置く場合のみ有効です。11月16日記事

目次





.htaccess自動生成サイト

超便利なサイトがあるんですね。 https://htaccess.cman.jp/explain/redirect_mobi.html

ここでスマートフォンのみリダイレクトさせる.htaccess

  1. チェックは「スマートフォン」のみ
  2. リダイレクト先は「m/」

生成

<Files ~ "^\.ht">
deny from all
</Files>

# Redirect UserAgent
RewriteEngine On

# DEVICE
SetEnvIfNoCase User-Agent "iPhone"                     ua_device=smart
SetEnvIfNoCase User-Agent "iPod"                       ua_device=smart
SetEnvIfNoCase User-Agent "Android.*Mobi"              ua_device=smart
SetEnvIfNoCase User-Agent "Windows Phone"              ua_device=smart
SetEnvIfNoCase User-Agent "Mobi.*Firefox"              ua_device=smart
SetEnvIfNoCase User-Agent "(Nexus 4|Nexus 5|Nexus 6)"  ua_device=smart
SetEnvIfNoCase User-Agent "BlackBerry"                 ua_device=smart
SetEnvIfNoCase User-Agent "iPad"                       ua_device=tablet
SetEnvIfNoCase User-Agent "Android.*Tablet"            ua_device=tablet
SetEnvIfNoCase User-Agent "Windows.*Touch"             ua_device=tablet
SetEnvIfNoCase User-Agent "Tablet.*Firefox"            ua_device=tablet
SetEnvIfNoCase User-Agent "(Kindle|Silk)"              ua_device=tablet
SetEnvIfNoCase User-Agent "(Nexus 7|Nexus 9|Nexus 10)" ua_device=tablet
SetEnvIfNoCase User-Agent "^DoCoMo"                    ua_device=phone
SetEnvIfNoCase User-Agent "UP.Browser"                 ua_device=phone
SetEnvIfNoCase User-Agent "SoftBank"                   ua_device=phone
SetEnvIfNoCase User-Agent "^J-PHONE"                   ua_device=phone
SetEnvIfNoCase User-Agent "MOT-"                       ua_device=phone
SetEnvIfNoCase User-Agent "WILLCOM"                    ua_device=phone
SetEnvIfNoCase User-Agent "^emobile"                   ua_device=phone

# base path
RewriteBase /

# ridirect path condition
RewriteCond %{REQUEST_URI} !^m/.*$
# user-agent condition
RewriteCond %{ENV:ua_device} smart
# redirect
RewriteRule ^(.*)$ m/$1 [R,L]



スマートフォンとタブレットにリダイレクト

  1. チェックは「スマートフォン」と「タブレット」のみ
  2. リダイレクト先は「m/」

生成

<Files ~ "^\.ht">
deny from all
</Files>

# Redirect UserAgent
RewriteEngine On

# DEVICE
SetEnvIfNoCase User-Agent "iPhone"                     ua_device=smart
SetEnvIfNoCase User-Agent "iPod"                       ua_device=smart
SetEnvIfNoCase User-Agent "Android.*Mobi"              ua_device=smart
SetEnvIfNoCase User-Agent "Windows Phone"              ua_device=smart
SetEnvIfNoCase User-Agent "Mobi.*Firefox"              ua_device=smart
SetEnvIfNoCase User-Agent "(Nexus 4|Nexus 5|Nexus 6)"  ua_device=smart
SetEnvIfNoCase User-Agent "BlackBerry"                 ua_device=smart
SetEnvIfNoCase User-Agent "iPad"                       ua_device=tablet
SetEnvIfNoCase User-Agent "Android.*Tablet"            ua_device=tablet
SetEnvIfNoCase User-Agent "Windows.*Touch"             ua_device=tablet
SetEnvIfNoCase User-Agent "Tablet.*Firefox"            ua_device=tablet
SetEnvIfNoCase User-Agent "(Kindle|Silk)"              ua_device=tablet
SetEnvIfNoCase User-Agent "(Nexus 7|Nexus 9|Nexus 10)" ua_device=tablet
SetEnvIfNoCase User-Agent "^DoCoMo"                    ua_device=phone
SetEnvIfNoCase User-Agent "UP.Browser"                 ua_device=phone
SetEnvIfNoCase User-Agent "SoftBank"                   ua_device=phone
SetEnvIfNoCase User-Agent "^J-PHONE"                   ua_device=phone
SetEnvIfNoCase User-Agent "MOT-"                       ua_device=phone
SetEnvIfNoCase User-Agent "WILLCOM"                    ua_device=phone
SetEnvIfNoCase User-Agent "^emobile"                   ua_device=phone

# base path
RewriteBase /

# ridirect path condition
RewriteCond %{REQUEST_URI} !^m/.*$
# user-agent condition
RewriteCond %{ENV:ua_device} smart [OR]
RewriteCond %{ENV:ua_device} tablet
# redirect
RewriteRule ^(.*)$ m/$1 [R,L]



これらをドキュメントルートに置く。

ドキュメントルートが/var/www/html/ならその直下に置きます。そしてそのディレクトリに「m」というディレクトリをおけばOKです。

便利ですね。

最終更新:2015年11月17日 21:59