プロが教えるわが家の防犯対策術!

.htaccess で PC からのアクセスを拒否したいのですが、
IP アドレス帯域で拒否する方法しか思い浮かばず、
古い帯域情報しかわからずです。

他に Chromeのデベロッパーでもスマホサイトをみれないように
.htaccess で PC からのアクセスを拒否する方法はありませんか ?

広告をサイトにつけてるのですが、PC では表示しないためです。

わかる方おりましたら、教えてください。

A 回答 (3件)

User-Agentで絞るとか。



SetEnvIfNoCase User-Agent "Windows" ua_os=windows
SetEnvIfNoCase User-Agent "Mac.*OS" ua_os=macos
SetEnvIfNoCase User-Agent "Linux" ua_os=linux

RewriteEngine On

RewriteCond %{ENV:ua_os} windows [OR]
RewriteCond %{ENV:ua_os} macos [OR]
RewriteCond %{ENV:ua_os} linux
RewriteRule ^(.*)$ - [F,L]
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

なるほど。
試してみます。

お礼日時:2021/09/21 12:03

スマートフォンとそれ以外の区別ならば、HTTP ヘッダ部の User-Agent (UA) を読み取ることで(たぶん)判別できます。


以下の説明によると、特定の文字列 “Mobi” が含まれていればモバイル端末だそうです。

参考) UA 文字列を用いたブラウザーの判定
https://developer.mozilla.org/ja/docs/Web/HTTP/B …

htaccess で検知させるならば、以下のようにすればよいでしょう

RewriteCond %{HTTP_USER_AGENT} !(Mobi)
RewriteRule ".*" "-" [F,L]

参考) htaccess の条件の書き方
https://httpd.apache.org/docs/2.4/mod/mod_rewrit …

デベロッパーとは開発用のエミュレーターのことでしょうか?
それならば UA を完全に偽装してくるので識別はできません。
あきらめましょう。

余談ではありますが

このようなやり方は一般に良くないとされていますので、できれば別の手段を探すなり、そもそもの仕様を再検討することをお勧めします。
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

お礼日時:2021/09/21 19:10

UAで禁止すればいい


https://htaccess.cman.jp/explain/access_useragen …
ただ、MACOSとIOSの区別がつかないかもしれんな

# AccessControl UserAgent

# OS
SetEnvIfNoCase User-Agent "Windows" ua_os=windows
SetEnvIfNoCase User-Agent "Mac.*OS" ua_os=macos
SetEnvIfNoCase User-Agent "Linux" ua_os=linux
SetEnvIfNoCase User-Agent "SunOS" ua_os=linux
SetEnvIfNoCase User-Agent "BSD" ua_os=linux
SetEnvIfNoCase User-Agent "Android" ua_os=android
SetEnvIfNoCase User-Agent "Sleipnir" ua_os=sleipnir
SetEnvIfNoCase User-Agent "Nintendo" ua_os=game
SetEnvIfNoCase User-Agent "PlayStation" ua_os=game

# 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

RewriteEngine On

RewriteCond %{ENV:ua_device} tablet [OR]
RewriteCond %{ENV:ua_os} windows [OR]
RewriteCond %{ENV:ua_os} linux [OR]
RewriteCond %{ENV:ua_os} game [OR]
RewriteCond %{ENV:ua_os} phone
RewriteRule ^(.*)$ - [F,L]
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

試してみます。

お礼日時:2021/09/21 12:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!