We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.

Your Digital Commerce Experts
Nexcess Logo

How to Use .htaccess Files

March 20, 2020

The .htaccess file is a configuration file for the Apache web server. You can add directives into the  servers .htaccess file  to restrict IPs, allow IPs, and redirect requests.

Sites will automatically have a .htaccess file. When you connect to your site using your FTP program you can find it in the public_html folder. 

Note: not all FTP programs allow you to see "hidden" files like .htaccess. If you aren't seeing your .htaccess file make sure that you enable "view hidden files" or something similar in your FTP program.

If you need to redirect users from an old URL that no longer points anywhere to a new page you can do that with your .htaccess file.

In order to be able to edit your sites .htaccess file you will need to have the SSH credentials for your server. You can then use any FTP program you like. If you’ve never used one we recommend CyberDuck if you are on a Mac or WinSCP if you’re on a PC.

To redirect from one URL to a new URL then you can create a 301 redirect in your htaccess. The following line redirects /account/ to /my-account/.

RewriteRule ^account/$ /my-account/ [R=301,L]

If you want to redirect the license.txt and the readme.html files to the sites homepage you can add the following to your htaccess file:

<IfModule mod_rewrite.c>
RewriteRule ^readme\.html$ / [R=301,L]
RewriteRule ^license\.txt$ / [R=301,L]
</IfModule>
To redirect non-www requests to www site. This would be when your site URL is set with a www in it and you want any non-www request to redirect to the www version.
# Re-write non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

To redirect www requests to a non-www site. This would be if your site URL is set without a www in it.

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

To make your site more secure you can redirect HTTP requests to HTTPS. 

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

If you are using a redirection plugin on your site like Redirection. Then the plugin has a feature to be able to export to .htaccess. You can create rewrite rules in the plugin and then export them.

Redirection > Import/Export then select Apache .htaccess


You can take the .htaccess redirection rules from the plugin and add them into the sites .htaccess file. Then you can delete those redirects in the plugin. 

Redirects will be faster when you use the .htaccess file instead of the redirect being stored in the sites database.

We've only touched on the some of the simplest examples. There's a lot more you can do with .htaccess file. This article gives you some common examples for the type of redirects that can be added into your sites .htaccess file.

Patrick Rauland