3 Things You Should Know About The Apache HTTP htaccess file – 2018

3 Things You Should Know About The Apache HTTP htaccess file – 2018

I have been using the Apache HTTP Server since early 2000’s, and it only continues to get better. While most people don’t need to know how to set it up from scratch and configure it, you should know how to use the htaccess file. If you create a file named “.htaccess” in the “public_html” folder of your website, you can do some really neat things. I’ll cover three things everyone using Apache should know.

Block Users With htaccess

You may find yourself wanting to block a certain visit from your site, and that’s okay, but how do you do it?

allow from all
deny from 1.2.3.4 # blocks one IP address
deny from 1.3 # blocks every IP beginning with 1.3

SEO Friendly URLs With htaccess

If you’re tired of having “index.php” in your URLs, I don’t blame you. Below is a snippet you can add to your htaccess file to easily rewrite your URLs to remove it. Doing it this way, you don’t have to change any of your code, just add the snippet below.

RewriteEngine On
RewriteBase /

# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)

Forcing HTTPS With htaccess

If your site has an SSL certificate, then I recommend you force your users to use it. That green “Secure” text in the address bar makes everyone feel a little safer, and it helps with SEO.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Conclusion

While nginx gets all the attention lately, we can’t forget about Apache HTTP Server because it’s going to be around for quite awhile. Be sure to check out the Apache docs or leave a question below.

If you are familiar with Apache but would like to try nginx, you could always use nginx as a reverse proxy.