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
</IfModule>
but it does not modify the url, that is, the current url is this example:
domain/folder/home.php
I want it to stay
domain/folder/home
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:
Let's explain the code a bit: First of all we have the line
RewriteEngine on
, which activates the modifications in the urls. In the second and third lines we have theRewriteCond
, the first prevents rules from matching directories and the second prevents them from matching files.And finally we have the line
RewriteRule
, with this we are going to go by 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 must 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, that follow the following form:$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:
In this way we could use the friendly URL to access.