.htaccess FAQ

Question Answer
What is .htaccess? .htaccess is a tool found on Apache server, allowing website admins to define rules, modify URL/URIs, change configuration, as well as enable, or disable functionalities of the server. For example, you can set error pages, directory password protection, IP-related restrictions, as well as controlling the URL redirection.
How do I use .htaccess? You can use .htaccess by opening a text-editing program, entering (Or copying) in .htaccess code, then saving to your webserver with the name of “.htaccess” (There is nothing before the dot). Learn more about .htaccess here.
How do I force HTTPS with .htaccess? Simple! Add this code to your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
IMPORTANT! The "RewriteEngine on" line should only be found ONCE in your entire .htaccess file. If it is needed, it should be located before any other statements (At the top of the file). Learn more on the official article here.
How do I set cache headers using .htaccess? Cache headers can be quite long to write, so we recommend that you check out our main article on the topic, located here.
How do I set the timezone of the server? That’s nice and easy! Add this code to your .htaccess file.
php_value date.timezone 'America/Chicago'
SetEnv TZ America/Chicago
Remember to replace “America/Chicago” with your timezone! Learn more here.
How do I block IP addresses on my website? Simply add this code to your .htaccess file. Don’t know what .htaccess is?
Order Allow,Deny
Deny from [IP here] [Next IP here] [Next IP here] [Next IP here]
Allow from all
You only need to supply one IP, but you can provide multiple! Click me to learn more, or to learn how to block a range of IPs.
How do I create custom file extensions on my website? Custom file extensions? .htaccess can do that easily. Just copy this code into your .htaccess file, and click me if you're not sure what .htaccess is.
RewriteEngine on
RewriteRule ^(.*)\.CUSTOM$ $1.php
IMPORTANT! The "RewriteEngine on" line should only be found ONCE in your entire .htaccess file. If it is needed, it should be located before any other statements (At the top of the file).
Note: Change “.php” to the real extension you want to change (.html, .png, ect) and .CUSTOM to the extension you want to use instead. Learn more about custom file extensions with htaccess here.
How do I remove the .html and .php on my website? Nice and easy with .htaccess! Click here if you don’t know what .htaccess is, otherwise add the code below to your .htaccess file!
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
IMPORTANT! The "RewriteEngine on" line should only be found ONCE in your entire .htaccess file. If it is needed, it should be located before any other statements (At the top of the file).
Please Note: The code above removes the .php file extension. To remove other file extensions, simply copy the code again and replace “.php” with the extension you want to remove. Learn more about removing file extensions with htaccess here.

Advertisement

Don't see what you are looking for? We have many more articles in the htaccess catagory.

Advertisement

Article by Tinkerman