Wednesday, March 18, 2020

Apache htaccess

In this post we are going to quickly go through some explanations and techniques relating to .htaccess files. This information should help you diagnose problems or make quick adjustments without having to know everything. .htaccess is an Apache feature, so this post applies to Apache servers, and you can check the Apache documentation for details not covered here. Other servers might also implement .htaccess files, but you would have to check for the particular brand.


.htaccess files...

The file must be named .htaccess exactly, nothing before the dot. If you are trying to make the file on Windows you might find you can't name a file that, Windows will show a message saying you need a name before the extention, that means text before the "dot". Consider getting a program called Notepad++, it can save files with the .htaccess name on Windows and is good for editing them. The file is just plain text, and is in theory a list of commands, each command taking up one line.

You upload the file to...

The place on the server where the .htaccess file goes depends on the folders and files you want it to have an effect on. The effect it has is based on the commands in it, effects such as password protection, redirects, or URL rewriting. These effects are applied to the folder the .htaccess file is in, and any subfolders including folders and files in them. Therefore a .htaccess file in your root directory affects the whole website. There are exceptions though, otherwise this would be a little inflexible. If you have another .htaccess file in a subfolder it overrides the previous one, and it's effect extends to subfolders too.

Your First Command

Here's a simple command you can put in a file to get you started, or to test if .htaccess is operating normally.

redirect /my-path/my-file.php http://mywebsite.com

Replace the first path with the full path of the file to redirect. Replace the second path, the URL after the next space, with your website URL. It should now redirect to the destination URL. There are numerous other tasks that can be performed with commands, such as URL rewrites, custom error pages, MIME type and extension overrides, and blocking particular bots (or referrers or IP addresses).

No comments:

Post a Comment