htaccess RewriteRule to deny access to opera mini browser | amitoverflow blog

A simple rewrite rule to forbid requests from opera mini browser.
If your website is designed for well known browsers like Chrome, sfari etc and you don't want visitors to access your website using Opera mini browser , you can use the following RewriteRule in your htaccess or server.config file to block access to this specific browser.

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ - [F]
This rule will redirect all requests from opera to your website to 403 forbidden error . Visitors who come to your website using opera will see a forbidden error.

Instead of showing a forbidden error ,if you want to redirect your opera visitors to a specific page on your site then you could use :
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ /demo.php [R]
This will redirect your opera visitors to /demo.php page on your site.

On your server you may have more then one domain pointing to the same document root. The rules above apply to all domains including subdomains if these are pointing to the same document root. In that case you can use a RewriteCond directive to match against the %{HTTP_HOST} header so that the rule can apply to a specific domain only.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ - [F]
The rule above will show a forbidden error for opera visitors and the rule applies only to www.example.com . This won't affect your other domains for example if you have www.example2.com using the same document root then this rule will not apply to your www.example2.com domain.

Post a Comment

0 Comments