.htaccess FAQ

QuestionAnswer
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 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 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 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.

Advertisement

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.

Article Author

Related Articles

Disable Image Hotlinking with .htaccess

It’s always a good idea to disable image hotlinking. Image hotlinking is when someone else displays your image on their website, but they embed it from your site. This means that visitors to this othe...

Deny Access to a Directory or File with .htaccess

Have your website all setup? Amazing! There might be a directory that you want no one to ever visit, not even yourself! Although this does not really make sense on a web server, here are the instructi...

Disable Directory Index Viewing

Have your website online, but don’t have an “index” file everywhere yet? Your visitors may be able to see your directory index page, and you don’t want that!

Deny Access to Hidden Files with .htaccess

Is your website ready to go live? It’s always a good idea to block people from accessing files they shouldn't, like your .htaccess file for example! This code will block access to any files beginning ...

Setting index pages with .htaccess

All you have to do is add this code into your .htaccess file and save it! Like all file changes, it can take some time before it updates everywhere, but you can speed up the process on your devices si...

Advertisement

All code and content © 2024. Contact Us to learn more.