Remove php / html extensions with .htaccess

4

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 be

  

domain / folder / home

    
asked by Johendry Parra 19.06.2018 в 16:22
source

1 answer

4

What you're trying to do is call friendly URLs and, if you're looking for a bit, there are a lot of tutorials in google, however, I'll try to explain it a bit:

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

  

www.laredsocialquesea.com/profile/pepe

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

  

www.laredsocialquesea.com/SearchProfile.php?nick=pepe

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 of all we have the line RewriteEngine on , which activates the changes in the urls. In the second and third lines we have the RewriteCond , the first avoids the rules that match directories and the second avoids matching files.

And finally we have the line of RewriteRule , with this we will 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% (.+) :

  

www.laredsocialquesea.com/profile/valor

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

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

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

  

www.laredsocialquesea.com/profile/pepe/photos/5

And the unfriendly Url would be like this:

  

www.laredsocialquesea.com/SearchProfile.php?nick=pepe&photoid=5

Therefore, the rule that should be written in the htaccess would be such that:

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

This way we could use the friendly URL to access.

    
answered by 19.06.2018 / 16:31
source