Joakim
Joakim

Reputation: 113

Is there a way to htaccess password protect www.somedomain/?data&some-url?

I want to secure www.somedomain/?data&some-url with htaccess and let the www.somedomain to work fine but when accessing that URL directly or from a link, the htaccess protects it.

Is it possible?


fine..

lets make this "easier"

my htaccess file is this but it wont work

<files ?some&data>
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /dir/passwd
require valid-user
</files>

Upvotes: 0

Views: 172

Answers (3)

anubhava
anubhava

Reputation: 785108

Here is how you can show auth dialogue for a particular query string.

RewriteEngine On
RewriteBase /

## force BASIC auth for a specific query parameter
#set PSSWD_NEEDED var to 1 if query string is matched
RewriteCond %{QUERY_STRING} (?:^|&)data&some-url(?:&|$) [NC]
RewriteRule ^(index\.php)?/?$ index.php/%{QUERY_STRING} [L]

# set PSSWD_NEEDED var to 1 if URI is /index.php/...
SetEnvIfNoCase Request_URI "^/index\.php/data&some-url" PSSWD_NEEDED

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /dir/passwd
require valid-user
Order allow,deny
Allow from all
Deny from env=PSSWD_NEEDED
Satisfy any

Upvotes: 0

Usman
Usman

Reputation: 53

Hi theres well written article about this

1: heres the Link of that article

I could write some of that text here and mention source but that some text may couldnt be helpful for all

Upvotes: 1

GandalfTheWhite
GandalfTheWhite

Reputation: 149

I suggest a workaround: you can use .htaccess to redirect that specific path (/?data&some-url) to an .htaccess protected dummy folder .e.g /myproctedtforlder/. Prix had a point by mentioning the QUERY_STRING condition. I modified my answer with full details. The first thing to do is rename index.php to whatever name you want, in this case I used index2.php.

So your main page's .htaccess would contain these lines:

RewriteCond %{QUERY_STRING} data&some-url
RewriteRule ^$ /myprotectedfolder/ [QSA,L] 

RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|jfif|bmp|css|js|txt|xml)$ [NC]  
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ index2.php [QSA,L] 

RewriteRule ^index\.php$ index2.php [QSA,L] 

and then your protected folder (/myprotectedfolder/) .htaccess would look like:

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/username/myprotectedfolder/.htpasswd
require valid-user

I am not giving further details about the .htpassword, you can find many examples like in here: http://davidwalsh.name/password-protect-directory-using-htaccess. In the 'answer' easy details you provided below, you should be careful that in tag you should provide file names format (e.g index.(php|html)) not the query string. In your case you are providing query string. So that's not working to start with. Could you please remove that 'answer' and put it as a comment?

Upvotes: 1

Related Questions