Remove php/html extensions with .htaccess


I want to remove php/html extensions with .htaccess I have this code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

but it does not modify the url, that is, the current url is this example:


I want it to stay



What you are trying to do is called establishing friendly URLs and, if you search a bit, there are many tutorials on google, however, I will try to explain it to you a bit:

First of all, an example of a friendly URL would be something like this:

An unfriendly URL would be, following the same example, something like this:

Knowing this, in the .htaccess file to create friendly URLs we could do something like this:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
Rewriterule ^profile/(.+)$ archivo.php?nick=$1

Let's explain the code a bit: First we have the RewriteEngine on line, which activates the modifications in the urls. On the second and third lines we have the RewriteCond , the first one prevents rules from matching directories and the second prevents them from matching files.

And finally we have the RewriteRule line, with this we are going to go in parts:

First of all we have the symbol ^ , which means that the expression begins.

Then we have profile/(.+) , which means that after the url there has to be a value, that is (.+) :

This value is determined after the $, and you can see that it is the name of the file, with its extension and everything, but with an addition, which is $1 .

$1 is going to be the name of the value that we want to access, and there may be more values ​​that you can add, going like this: $2 , $3 … etc. Each value you add is equivalent to a (.+) in the previous expression.

So, as a last example, let's say we want to access a profile picture of our fictional character pepe directly. The friendly URL would be something like this:

And the unfriendly Url would be like this:

Therefore, the rule that should be written in the htaccess would be something like this:

Rewriterule ^carpeta/(.+)$ $1.php

In this way we could use the friendly URL to access.

Scroll to Top