mod_rewrite and htaccess password protection together - .htaccess

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>

Related

Redirect one URL to another using htaccess

My current .htaccess file code is as follows:
### ISPConfig folder protection begin ###
AuthType Basic
AuthName "Members Only"
AuthUserFile /var/www/web113/web/.htpasswd
require valid-user
### ISPConfig folder protection end ###
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
I want to redirect URL as http://xxxxx.xxxxxx.net/local-attraction to http://xxxxx.xxxxx.net/pages/local-attraction
I have tried with
RedirectPermanent http://xxxxx.xxxxxx.net/local-attraction http://xxxxx.xxxxx.net/pages/local-attraction
Also with the
Redirect 301 /local-attraction http://xxxxx.xxxxx.net/pages/local-attraction
But nothing works, it is showing 404 page.
You should use mod_rewrite rule for this redirect since you are already using it your .htaccess:
RewriteEngine On
RewriteRule ^local-attraction/?$ /pages/$0 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

.htaccess how to redirect domain except for home page & subdirectory

From this answer on how to redirect a domain except for a subdirectory, and this answer on how to redirect a domain except for the homepage, I have ended up with the following in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?this\.example.com$ [NC]
RewriteRule !^support !^directory-1 !^directory-2 (.+) https://www.this2.com%{REQUEST_URI} [NE,NC,R=301,L]
</IfModule>
However, this.example.com/work is not redirecting to www.this2.com/work.
Help appreciated.
Update, from anubhava's answer, this is my new .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^doig\.website\.technology$ [NC]
RewriteRule !^($|support|dynamic-keyword-module-drupal8|dynamic-keyword-plugin-joomla) https://www.webby.net.au%{REQUEST_URI} [NE,NC,R=301,L]
</IfModule>
However, https://doig.website.technology/my-work/ is not redirecting.
Update 2: this is my full .htaccess:
# Redirects
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^doig\.website\.technology$ [NC]
RewriteRule !^($|support|dynamic-keyword-module-drupal8|dynamic-keyword-plugin-joomla|wp-content|wp-includes|wp-admin) https://www.webby.net.au%{REQUEST_URI} [NE,NC,R=301,L]
</IfModule>
# BEGIN LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
RewriteRule .* - [E=Cache-Control:no-autoflush]
RewriteRule min/\w+\.(css|js) - [E=cache-control:no-vary]
</IfModule>
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END LSCACHE
# BEGIN litespeed noabort
<IfModule rewrite_module>
RewriteEngine On
RewriteRule .* - [E=noabort:1]
</IfModule>
# END litespeed noabort
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteRule ^support\/?(.*)$ "https\:\/\/doig\.website\.technology\/support\/$1" [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} ^/?wp\-content/+debug\.log$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule .* - [F,L,NC]
</IfModule>
<IfModule !mod_rewrite.c>
<Files "debug.log">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
</IfModule>
# BEGIN Wordpress (new)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
</IfModule>
# END Wordpress
# BEGIN MainWP
# END MainWP
# Wordfence WAF
<IfModule LiteSpeed>
php_value auto_prepend_file '/home3/user/public_html/wordfence-waf.php'
</IfModule>
<IfModule lsapi_module>
php_value auto_prepend_file '/home3/user/public_html/wordfence-waf.php'
</IfModule>
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
# END Wordfence WAF
You may use this rule with corrected syntax of consitions:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^doig\.website\.technology$ [NC]
RewriteCond %{THE_REQUEST} !/(min|support|dynamic-keyword-module-drupal8|dynamic-keyword-plugin-joomla|wp-content|wp-includes|wp-admin)/ [NC]
RewriteRule . https://www.webby.net.au%{REQUEST_URI} [NE,R=301,L]

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"

How to protect a specific url through 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

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]

Resources