Control file extensions with an .htaccess file

 

Overview

This article provides code examples to control file extensions on your website.

Creating an .htaccess file on your DreamHost web server

See this article for instructions on how to create an .htaccess file on your web server.

If the file already exists, see the following articles for instructions on how to update it (depending on if you're using an FTP client or SSH):

How to control a file extension

The examples below provide several options to adjust file extensions on a website.

These code examples can be entered into your .htaccess file exactly as shown.

You only need to update the highlighted code to your actual URL. For example, if you see the domain example.com, change this to your own domain name.

Changing the file extension

This example allows you to use a .zig extension in addition to the regular .php extension. So, you could access a file at example.zig as well as example.php:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.zig$ /$1.php [NC,L]

Forcing other file extensions to load as PHP

See this article for several examples of how to force any file extension to load as a .php file.

Removing the file extension

The following completely removes the file extension from your URL. So, example.php would appear as example. This example is for .php files, but you can use it for any other type by replacing .php with your desired extension:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\ HTTP/
RewriteRule ^(.*)index$ http://example.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.php\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [L,R=301]
RewriteRule ^([a-z]+)$ /$1.php [L]

Allowing a file to load without the extension

This example does not automatically remove the file extension. However, it does allow the file to be loaded without the extension. So, if example.com/test.php exists, you can then load it as example.com/test:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?