How to protect a specific url through htaccess - .htaccess

I want to add a login restriction on certain page like.
www.example.com/testing
whenever someone hit this url it should ask password, otherwise whole website should work password free.
Htaccess Code:
SetEnvIfNoCase Request_URI ^/testing-softwares SECURED
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user
Satisfy any
Order Allow,Deny
Allow from all
Deny from env=SECURED
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /nrl_shareware/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abc_shareware/index.php [L]
</IfModule>
# END WordPress
Htpasswd Code:
test:$apr1$O9AK9s5s$H0IuOqkTnB0yJ5k35kX2a1
Updated Code
SetEnvIfNoCase Request_URI ^/testing-softwares SECURED
AuthName "Restricted Area"
AuthType Basic
AuthUserFile "D:/DONT DELETE 80-261209/ABCProjects/xampp/htdocs/abc_shareware/.htpasswd"
AuthGroupFile /
Require valid-user
Satisfy any
Order Allow,Deny
Allow from all
Deny from env=SECURED
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /abc_shareware/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abc_shareware/index.php [L]
</IfModule>
# END WordPress

You can use it this way with mod_setenvif:
SetEnvIfNoCase Request_URI ^/testing SECURED
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user
Satisfy any
Order Allow,Deny
Allow from all
Deny from env=SECURED

Related

Code Ingnitor - Home page works.. All other pages have 404 error

When accessing from ######1.co.uk (which has an SSL) to document root ###/production everything works fine.
However when accessing ###/production from ######2.co.uk (which doesn't have an SSL - set up in the vhost file) the home page displays fine but the other pages are returning a 404.
Here's the entry in the vhost file that points #####2.co.uk to ###/production
<VirtualHost *:80>
DocumentRoot "/var/www/html/####/production"
ServerName #####2.co.uk
ErrorLog "/etc/httpd/logs/####.error.log"
CustomLog "/etc/httpd/logs/####.log" common
<Directory /var/www/html/####/preprod>
AuthType Basic
AuthName "Authentication Required"
AuthUserFile "/var/www/html/secure/.htpasswd"
Require valid-user
order allow,deny
allow from all
AllowOverride all
</Directory>
<Location /api/sms>
AuthType Basic
AuthName "Authentication Required"
AuthUserFile "/var/www/html/secure/.htpasswd"
Require valid-user
</Location>
</VirtualHost>
Done a quick google search and this could be down to the .htaccess file in ###/production
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Old website redirects
RewriteRule ^(.+)\.aspx$ http://%{SERVER_NAME}/$1/ [QSA,NC,R=301,L]
RewriteRule default/?$ http://%{SERVER_NAME}/home/ [NC,R=301,L]
#SSL Redirects
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L]
# Codigniter Page Handling
RewriteCond $1 !^(index\.php|content|assets|files|flash|uploads|xml|private/uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any ideas as to what the problem is?
Also note that the Directory of #####2.co.uk is set to
<Directory /var/www/html/####/preprod>
Which is different to the Document Root.
DocumentRoot "/var/www/html/####/production"

mod_rewrite and htaccess password protection together

I am currently trying to combine the two - htaccess password protection and mod_rewrite. My problem is, that the code below is resulting an error: "No input file specified."
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile .htpasswd
Require valid-user
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Can anyone see, what I am doing wrong?
EDIT #1:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /.htpasswd
Require valid-user
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>
Make sure AuthUserFile has full path to a valid encrypted passwords file.
Rest of the code, have it like this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /full/path/to/.htpasswd
Require valid-user
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^((?!public/).*)$ /public/$1 [L,NC]
</IfModule>

how to call index.php as the default, but only for a specific domain?

I have a few sites hosted on the same server,
but there is only one htaccess file that i guess governs all of them.
so how can i make index.php the default instead of index.html for only one of these sites?
thanks!
# -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName site1.com
AuthUserFile /home/myuser/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/myuser/public_html/_vti_pvt/service.grp
AddHandler server-parsed .shtml
AddHandler server-parsed .html
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site1\.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com$ [NC]
RewriteRule ^(index\.html)?$ /index.php [L,NC]`
For any particular site www.site.com have this rule in your .htaccess:
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^(index\.html)?$ /index.php [L,NC]
Suggested .htaccess:
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site1\.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com$ [NC]
RewriteRule ^(index\.html)?$ /index.php [L,NC]
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName site1.com
AuthUserFile /home/myuser/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/myuser/public_html/_vti_pvt/service.grp
AddHandler server-parsed .shtml
AddHandler server-parsed .html

htaccess - rewriting subdomain is breaking relative paths

My Setup using cPanel:
domain.com(/public_html/)
mysub.domain.com(/public_html/2013/sitepath/)
Goal: for sub2.mysub.domain.com/save-path-info to redirect to mysub.domain.com/save-path-info without change the url from sub2.mysub.domain.com. In other words, I want anysub.mysub.domain.com to be virtually the exact same as mysub.domain.com.
Code:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.2013.cqueue\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.2013.cqueue\.com$ [NC]
RewriteRule (.*) /2013/sitepath/$1 [L]
The code above was originally in the root directory of mysub.domain.com but it wasn't working until I moved it to the root directory of domain.com. It successfully keeps the URL and path, however it is breaking the relative path of my templates.
Example: templates normally look for the path to be / but, with the rewrite above the templates think they need to look in /2013/sitepath/
EDIT
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.mysub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://mysub.domain.com/$1 [L]
# -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName domain.com
AuthUserFile /home/uadmin/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/uadmin/public_html/_vti_pvt/service.grp
Try this code :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.mysub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://mysub.domain.com/$1 [L]

htaccess redirect to subdomain and keep string

Found a few similar questions but either I cant figure it out from the answers or they dont directly match my needs.
What I am trying to achieve is this:
I recently moved part of my site to a sub domain on a different server so what used to be in: http://mysite.com/clients is now on http://clients.mysite.com
using: RedirectMatch 301 https?://mysite.com/clients/^(.*)$ https://clients.mysite.com/$1 works fine for simply redirecting people hitting http://mysite.com/clients to the new sub domain however I would like to be able to preserve the string after /clients/
so if someone hits e.g. http://mysite.com/clients/could_be_a_number_of_things.php it redirects to http://clients.mysite.com/could_be_a_number_of_things.php
Can anyone advise?
full .htaccess as requested:
# -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
#AuthName mysite.com
#AuthUserFile /home/inhost/public_html/_vti_pvt/service.pwd
#AuthGroupFile /home/inhost/public_html/_vti_pvt/service.grp
# BEGIN WordPress
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteRule ^clients/(.*)$ http%1://clients.mysite.com/$1 [L,NC,R=301]
</IfModule>
# END WordPress
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory on mysite.com server:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteRule ^clients/(.*)$ http%1://clients.mysite.com/$1 [L,NC,R=301]

Resources