Blocking Browsers based on HTTP_USER_AGENT in Apache server @ centos
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Blocking Browsers based on HTTP_USER_AGENT in Apache server @ centos
I am using Centos 7 and installed Apache server with an index.html page in it, the situation is i want to restrict the access for different browsers such as Internet explorer, Chrome and allow using Firefox to access the index.html page, i've created a ".htaccess" file and made the necessary changes which i've written at the bottom of my thread. I googled that we can write using rewrite rules and tried a lot of them, but couldn't get the exact code which helps me to get what i'm looking for, so i'm hoping someone in this forum can help me with my coding, i've executed various ways such as
RewriteEngine on
RewriteCond "%{HTTP_USER_AGENT}" "^Mozilla/3.*"
RewriteRule "^index\.html" "nopage.html" [F]
If it's your server then the changes need to go in the actual configuration file for your virtual host. The .htaccess files are a relic from the mid 1990's when shared hosting was common and necessary but lacked any possibility of virtual hosts. So if you are following a guide that recommends .htaccess, it is sorely out of date. Try using the configuration file that corresponds to the virtual host you are using.
Thank you @bathory for responding soon and thanks for your answer but unluckily when accessing the url of my apache server in Chrome, Firefox and Internet Explorer, it is showing the index.html page i've created, if you don't mind could you please explain what's happening in the code which you've given. thank you
RewriteEngine on
RewriteCond "%{HTTP_USER_AGENT}" "!(.*)Firefox" [NC]
RewriteRule index.html - [F]
The first step is hopefull self-evident: it enables the runtime URL re-writing capabilities. It only needs to be done once.
The second and third lines are defining the actual rewriting.
The RewriteCond directive sets up a pattern which when found triggers the subsequent RewriteRule As you can see in the pattern, !(.*)Firefox, it's looking for a HTTP request where the variable HTTP_USER_AGENT does not have the string "Firefox" The flags NC mean the pattern is sought without regard to case. The line might also be use like this:
Code:
RewriteCond "%{HTTP_USER_AGENT}" "!Firefox" [NC]
The RewriteRule directive does a replacement of the URL based on the pattern given. It could probably be rewritten so:
Code:
RewriteRule "^/index.html$" "/nopage.html" [L]
The L flag says to stop rewriting with that. No more rewrites after the line with the L.
See the authoritative documentation for more details:
Thank you @bathory for responding soon and thanks for your answer but unluckily when accessing the url of my apache server in Chrome, Firefox and Internet Explorer, it is showing the index.html page i've created, if you don't mind could you please explain what's happening in the code which you've given. thank you
Turbocapitalist has explained to you the rewrite code.
In fact it should give a 403 (Forbidden) error if the User-agent string does not contain "Firefox".
Perhaps you need to clear your browsers' cache to verify if it works.
If it still doesn't work, assuming that you used my code in a .htaccess, you need to check for the following:
- apache reads .htaccess files? You need an "AllowOverride All" directive for this.
- You put the .htaccess in the DocumentRoot of your apache server?
Thank you for your reply @Turbocapitalist, can you please guide me how to make it possible using the virtual hosts file, i guess we can try it using Allow and Deny policies but i'm not much sure about it, it could be more helpful if you come up with it and thank you for your time
Last edited by visuharsha; 07-26-2017 at 05:37 AM.
It's best for us and for you if we can get you standing on your own two feet.
Your re-write rules would go in the appropriate Apache2 (httpd) configuration file. If you are on CentOS 7 then you can find your Apache2 configuration files in the directory /etc/httpd/conf.d/ but if you've not added any files, then that is the place to start by creating one. Usually you make one file per virtual host. If you will only ever have one virtual host, then you are probably still using the default, which is /etc/httpd/conf/httpd.conf but it's more practical to plan ahead and make a separate virtual host file anyway. That way it's easy to expand when you need to.
Last edited by Turbocapitalist; 07-26-2017 at 05:57 AM.
for the code that does the deed.
This code, as stated should be in site.conf first (if accessible) and $DocumentRoot/ in .htacess if site.conf is not accessible.
Shared Hosting vs. Dedicated, or V{D,P}S
using .htaccess to do this is resource intensive and is not recommended.
Thanks a lot guys, found the solution for it, but little more help needed from you guys, we're using Firefox for Mozilla firefox, what should be written if we use Internet explorer? " MSIE " or " IE "
Thanks a lot guys, found the solution for it, but little more help needed from you guys, we're using Firefox for Mozilla firefox, what should be written if we use Internet explorer? " MSIE " or " IE "
The user agent string used by the legacy browsers probably vary by version, so you may need several patterns. You may want to connect several RewriteCond directives using an [OR] flag.
Check your logs to see what those strings really are or else you will have to guess which ones from a database like the one here over User Agent Strings listed by Browser.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.