LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-25-2017, 10:53 AM   #1
visuharsha
LQ Newbie
 
Registered: Jul 2017
Posts: 13

Rep: Reputation: Disabled
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]

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} \ Firefox/55 [NC]
RewriteRule ^ - [F,L]

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Firefox [NC]
RewriteRule .* - [F,L]

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Firefox/5\.0
RewriteRule - [L,R=403,E=Firefox]

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "MSIE 12.0" [NC,OR]
RewriteRule ^(.*)$ http://192.168.12.60/ [L,R=301]

Thanks in advance
 
Old 07-25-2017, 11:11 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by visuharsha View Post
i've created a ".htaccess" file
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.
 
Old 07-25-2017, 12:34 PM   #3
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
@OP

Regardless you're using .htaccess or the apache config files, you can use the following to allow access to index.html only for firefox:
Code:
RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} !(.*)Firefox [NC]
RewriteRule index.html - [F]
Regards

Last edited by bathory; 07-25-2017 at 01:02 PM.
 
Old 07-26-2017, 01:48 AM   #4
visuharsha
LQ Newbie
 
Registered: Jul 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-26-2017, 02:25 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Code:
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:
But have you moved your rewriting directives back into your virtual host's own configuration file and out of .htaccess yet?
 
Old 07-26-2017, 04:56 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by visuharsha View Post
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?
 
Old 07-26-2017, 05:35 AM   #7
visuharsha
LQ Newbie
 
Registered: Jul 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-26-2017, 05:38 AM   #8
visuharsha
LQ Newbie
 
Registered: Jul 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
Surely i'll check whatever the changes needed to be made @Turbocapitalist and will get back to you if there's any new update regarding this.
 
Old 07-26-2017, 05:56 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 07-26-2017, 06:56 AM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
.htaccess tricks and tips...Part I
.htaccess tricks and tips... Part II

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.

Last edited by Habitual; 07-26-2017 at 06:58 AM.
 
Old 07-26-2017, 08:14 AM   #11
visuharsha
LQ Newbie
 
Registered: Jul 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
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 "
 
Old 07-26-2017, 08:39 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by visuharsha View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permission configuration for a Centos-based web server oxnume Linux - Server 5 01-27-2017 09:17 PM
which version of Centos 6.5 can i use for my android based application's server. tchofo Linux - Newbie 1 07-28-2014 03:53 AM
problem in hosting apache based https server on centos 6.3!! diaablo2 Linux - Newbie 4 11-19-2013 02:50 AM
[SOLVED] Oracle application server based on CentOS anwar-it0 Linux - Server 4 05-21-2013 05:17 PM
Blocking iPod browsers on squid crackyblue Linux - Software 1 09-02-2010 03:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration