Difference between revisions of ".htaccess"

From Hostek.com Wiki
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Can I use a .htaccess file to password protect a directory?==
 
==Can I use a .htaccess file to password protect a directory?==
 
Yes, on our LINUX servers you can use a .htaccess file to password protect a directory.
 
Yes, on our LINUX servers you can use a .htaccess file to password protect a directory.
 +
 +
The code to perform this is:
 +
<pre>
 +
AuthUserFile /usr/local/you/safedir/.htpasswd
 +
AuthGroupFile /dev/null
 +
AuthName EnterPassword
 +
AuthType Basic
 +
 +
require user wsabstract
 +
</pre>
 +
 +
Change the File directory to the directory listed in your File Manager.
  
 
==Enabling Case Insensitive URLs for my Linux site==
 
==Enabling Case Insensitive URLs for my Linux site==
Line 6: Line 18:
 
<pre>CheckSpelling on</pre>
 
<pre>CheckSpelling on</pre>
  
 +
==Rewriting all URL requests to WWW.==
 +
Add the following to your .htaccess code:
 +
<pre>
 +
RewriteEngine On
 +
RewriteCond %{HTTP_HOST} !^www\.
 +
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
 +
</pre>
 +
 +
==Rewriting HTTP requests to HTTPS==
 +
Add the following to your .htaccess code:
 +
<pre>
 +
RewriteEngine On
 +
RewriteCond %{HTTPS} !^on$
 +
RewriteRule (.*) https://yourdomain/$1 [R,L]
 +
</pre>
 +
 +
==Create a Wildcard Subdomain Redirect==
 +
Add the following to your .htaccess code:
 +
<pre>
 +
RewriteEngine On
 +
RewriteCond %{HTTP_HOST} !^www\.mydomain\.dk$ [NC]
 +
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain.dk$ [NC]
 +
RewriteRule ^(.*)$ http://mydomain.dk/ [P,L,QSA]
 +
</pre>
 +
 +
==Create an IP-based Whitelist to a WordPress login page==
 +
Create a file called '.htaccess' in the same directory as the 'wp-login.php' page. Add the following code:
 +
<pre>
 +
Order Allow,Deny
 +
Allow from 111.222.333.444
 +
</pre>
 +
Replace 111.222.333.444 with your IP (you can find your IP at http://hostek.com/IP. Note that you can add multiple IP addresses like so:
 +
<pre>
 +
Order Allow,Deny
 +
Allow from 111.222.333.444
 +
Allow from 111.222.333.444
 +
Allow from 111.222.333.444
 +
Allow from 111.222.333.444
 +
</pre>
 +
 +
==Create a password to access the WordPress login page==
 +
Go to the directory outside of your website's public_html folder and create a file called .htpasswd. Within it, add the following:
 +
<pre>
 +
wp-user:$apr1$6DKxmMhU$liMLjMbLcQ1JmgLZix5l1/
 +
</pre>
 +
'''Note:''' The above file makes any login prompt that uses it have the username 'wp-user' and the password 'wp-password'. You can create your own login info to enter into the above file by [http://www.htaccesstools.com/htpasswd-generator/ clicking here and generating it.]<br />
 +
<br />Next, create a file called '.htaccess' in the same directory as the 'wp-login.php' page (usually /public_html). Add the following code:
 +
<pre>
 +
<FilesMatch "wp-login.php">
 +
AuthType Basic
 +
AuthName "Please Enter the WordPress Access Password:"
 +
AuthUserFile /home/username/.htpasswd
 +
Require valid-user
 +
</FilesMatch>
 +
</pre>
 +
The above info will now cause any request to the WordPress login page refer to the password file we just created. Replace 'username' with your cPanel username, usually found on the left side of your cPanel control panel home page.
 
[[Category:Linux]]
 
[[Category:Linux]]

Latest revision as of 16:11, 24 May 2017

Can I use a .htaccess file to password protect a directory?

Yes, on our LINUX servers you can use a .htaccess file to password protect a directory.

The code to perform this is:

AuthUserFile /usr/local/you/safedir/.htpasswd
AuthGroupFile /dev/null
AuthName EnterPassword
AuthType Basic

require user wsabstract

Change the File directory to the directory listed in your File Manager.

Enabling Case Insensitive URLs for my Linux site

Add the following to your .htaccess file:

CheckSpelling on

Rewriting all URL requests to WWW.

Add the following to your .htaccess code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Rewriting HTTP requests to HTTPS

Add the following to your .htaccess code:

RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://yourdomain/$1 [R,L]

Create a Wildcard Subdomain Redirect

Add the following to your .htaccess code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.dk$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain.dk$ [NC]
RewriteRule ^(.*)$ http://mydomain.dk/ [P,L,QSA]

Create an IP-based Whitelist to a WordPress login page

Create a file called '.htaccess' in the same directory as the 'wp-login.php' page. Add the following code:

Order Allow,Deny
Allow from 111.222.333.444

Replace 111.222.333.444 with your IP (you can find your IP at http://hostek.com/IP. Note that you can add multiple IP addresses like so:

Order Allow,Deny
Allow from 111.222.333.444
Allow from 111.222.333.444
Allow from 111.222.333.444
Allow from 111.222.333.444

Create a password to access the WordPress login page

Go to the directory outside of your website's public_html folder and create a file called .htpasswd. Within it, add the following:

wp-user:$apr1$6DKxmMhU$liMLjMbLcQ1JmgLZix5l1/

Note: The above file makes any login prompt that uses it have the username 'wp-user' and the password 'wp-password'. You can create your own login info to enter into the above file by clicking here and generating it.

Next, create a file called '.htaccess' in the same directory as the 'wp-login.php' page (usually /public_html). Add the following code:

<FilesMatch "wp-login.php">
AuthType Basic
AuthName "Please Enter the WordPress Access Password:"
AuthUserFile /home/username/.htpasswd
Require valid-user
</FilesMatch>

The above info will now cause any request to the WordPress login page refer to the password file we just created. Replace 'username' with your cPanel username, usually found on the left side of your cPanel control panel home page.