EnexoOnoma
EnexoOnoma

Reputation: 8836

htaccess to allow only visits from ip but whitelist a dir and a file?

I want to add a htaccess to allow visits from a specific ip.

The tree goes like this

domain
  /abc/
  /def/

I want to restrict the folder /abc/ but whitelist the folder /def/

Also, on the /abc/ there is a specific file called ghi.php. Can I allow access to that specific file ?

How can I do this?

This is what i have in /abc/ that redirects everyone who is not into the specified ip. However, I want to allow access to the ghi.php inside that dir.

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^125\.17\.119\.16$
RewriteRule ^ http://www.domain.com/ [R]

Upvotes: 2

Views: 124

Answers (2)

Dennis B.
Dennis B.

Reputation: 1577

I would not use mod_rewrite for content protection, use the modules that are created for this:

# Default: deny all
Order Allow,Deny
# Allow the IP for everything
Allow from 125.17.119.16

# Allow access to this one PHP file
<Files /abc/ghi.php>
    Allow from all
</Files>

# Allow access to everything inside that folder
<FilesMatch "^/def/">
    Allow from all
</FilesMatch>

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41219

Try :

RewriteEngine on
#Allow access to the file
RewriteRule ^abc/ghi\.php$ - [L]
#forbid the request for /abc/* if ip not=1.2.3.4.5
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4\.5$
RewriteRule ^abc - [F,L]

If you want to redirect the request to homepage, just change the line

RewriteRule ^abc - [F,L]

to

RewriteRule ^abc http://example.com/ [L,R]

Upvotes: 0

Related Questions