5 great .htaccess tricks that you can’t live without

Jon Packziński
Sep 4, 2018 · 3 min read
Among other rules, the .htaccess file above will remove the www from all website addresses and will also force the secure https: version of the site. e.g. http://www.website.com would result in https://website.com.

The .htaccess file is an extremely powerful tool for your web development arsenal. Learning how to use it effectively will not only improve security but will improve functionality as well.

Whether it’s redirecting visitors to a temporary site, displaying a custom 404 error page, or even blocking specific IP Addresses from being able to view your site, here are 5 sets of .htaccess directives that every webmaster should know.

It is important to remember that editing the .htaccess file can bring your entire site down. Be sure to have a working back-up before experimenting.

Redirect Visitors While You Update Your Site

Update and test your site while visitors are redirected to the page of your choice:

order deny,allow
deny from all
allow from 192.192.192.192
ErrorDocument 403 /visitors-page.htmlallow from all

Replace 192.192.192.192 with your IP address. Also replace visitors-page.html with the name of the page you want visitors to see.

This allows only your ip address to access the actual website, and all other ip addresses will be redirected to the page of your choice. Pretty slick!

Display a Custom 404 Error Page

Your server displays a “404 File Not Found” error page whenever a visitor tries to access a page on your site that doesn’t exist.You can replace the server’s default error page with one of your own that explains the error in plain language and links visitors to your home page. Here’s how to use your own page:

ErrorDocument 404 /404.html

Replace 404.html with the name of the page that you want your visitors to see.

Handle Moved or Renamed Pages

This is a trick that I use a lot when I develop a new site. The clients old URL pattern most likely will not match mine, therefore I am able to use a 301 redirect to match and redirect the old URL pattern to the new

Redirect 301 /old.html http://yoursite.com/new.html

Using a 301 redirect also ensures the page doesn’t lose its search engine ranking.

Prevent Directory Browsing

When there’s no index page in a directory, visitors can look and see what’s inside. Most servers are configured to prevent directory browsing like this. However, if yours isn’t, here’s how to set it up:

Options All -Indexes

Using this will ensure that visitors or even search engines will not see files that are not meant for their eyes.

Create User Friendly URLs

This is one of my favorites. I’ve created many user friendly URLs whether it be to make it easy to remember or simply to clean up a long URL with .htaccess and an Apache module called mod_rewrite, you can set up URLs however you want. Your server can show the contents of “/pages/about-us.html” whenever anyone visits “http://yoursite.com/about”. Here are a few different types of examples:

RewriteEngine on
RewriteRule ^about/$ /pages/about.html [L]
RewriteRule ^features/$ /features.php [L]
RewriteRule ^issues/$ /issues.html [L]
RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteRule ^facebook/$ http://facebook.com/myfacebookpage [L]

Notice the last one. What I did there was made it so that instead of going to http://facebook.com/myfacebookpage, you can advertise as http://mywebsite.com/facebook.

I hope these tricks help you out! Remember, there is a lot more that you can do with mod_rewrite and .htaccess, I encourage you to explore some more.

Jon Packziński

Written by

Accomplished Information Technology Director | Project Manager | Digital Internet Marketing Professional | Web Developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade