This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Introducing Cloudways Community Forum. Start Discussions and Get Instant Help From Cloudways Community. JOIN NOW
Enhance your managed hosting experience with fast performance, 24x7 support and managed security. Start for Free

What Can I Do With an .htaccess File?September 23, 2017

 

In this KB

  • Cloudways console server settings section
  • How to add suggestions for new server settings
  • How to control items in 3 categories:
    • Authentication/Security
    • Rewriting/Redirection
    • Overriding some PHP and other useful parameters via .htaccess

 

Cloudways Platform users can easily control an important number of server settings via the Server Settings section (in the Server Management area). We are always adding new settings there and you can leave suggestions on our feedback page, so we can look into them.

However, there are additional settings or other tasks that you want to accomplish that are not currently supported via our Platform. This is where the .htaccess file comes into place and will allow you to further customize your Cloudways experience.

SSH into Your Server

The best way to create/edit a .htaccess file for your application is via an SSH session. Open a session to your server with the credentials of the application you want to create/edit the .htaccess for. Here you have SSH related KBs:

How to Connect to Your Server using SSH

How to Launch an SSH Terminal from Cloudways Platform

.htaccess Access/Security Tips

Here we are going to list a number of actions you can achieve via the .htaccess file to control access/protect your site. You may want to do this during development and before your site is publicly available or if your application is intended for a limited audience so will need to keep a tight control on access:

1) Password Protect Your Application

a) Find the path of the public_html folder of your application by running pwd from the shell.

b) Add in the .htaccess file following code (update AuthUserFile to your path above):

c) Create the password file .htpasswd and add a user using the following code (make sure you are in the public_html folder of the application you want to password protect):

From now on, you will have to provide the user and password you have set above to access your site. Revert the changes to open access again.

2) Block Access to Certain IP(s)

You may want to do this i.e. if you got attacked from a specific IP,  you will need to add the following code in the .htaccess file of the desired application (changes the IPs for your target ones):

3) Block All IP(s) With Few Exceptions

You could do this again during development. Again, replace IP(s) with your desired ones:

.htaccess Rewriting/Redirection Tips

There are different types of URL rewrite/redirection we can achieve via the .htaccess file. Here we are describing some of the essential. Just add the listed code to the .htaccess of your target application (update the URL to match your application one):

1. 301 (Permanent) Redirect

2. 302 (Temporary) Redirect

3. Force the URL to www

4. Force the URL to non-www

5. Force HTTPS

6. Force HTTP

7. Redirection of Domain to a Subdirectory

.htaccess PHP Settings and Other Items

You can control many PHP settings via entries in the .htaccess file, some examples are below. We have also added some other miscellaneous items in this list.

Note: For new servers (deployed from August 23, 2016), use PHP settings editor for PHP directives.

1) Editing the Post Max Size

2) Enabling PHP Directory Listing
3) Setting Alternate Directory Index Pages
4) Enabling mod_rewrite
5) Setting Max Input Time
6) Disable Logging of Repeated Errors
7) Disable Max Error String Length
8) Change the Default Character Set for PHP (Replace * with charset value)
9) Stop PHP Scripts from Executing in a Directory:
10) max_input_vars:
11) Disable APC Cache:
Please insert Content
Have Questions? Ask From Our Helpful Community

Comments are closed.

Do you still need help?

Our Technical Support team is always at work.