Remove file extension, not to be seen by user

Asked

Viewed 754 times

6

How would I make the file extension not be viewed by the user?

Example

Once the user finishes the registration, it will be directed to the page confirmacao.php, but I would like the extension .php were not visible, only confirmacao/. I understand that this is Url friendly and can be done by .htaccess, but how can I apply on all pages?

  • contato.php => contato/
  • cadastro.php => cadastro/
  • etc..

Is that possible? Or would I have to create a directory for each page?

1 answer

4


In my . htaccess I use this snippet of code to remove . php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

Explanation

  • %{REQUEST_FILENAME} - will check from the directory ROOT + o "arquivo" da url solicitada (you mean that you exclude Querystring)

Example

  • Your website is at /www/ (Directory ROOT design)
  • The requested url is domain.com/contato?name=Guilherme
  • %{REQUEST_FILENAME} will be /www/contato

Resuming

  • In the rule RewriteCond %{REQUEST_FILENAME}.php -f says : Take this path (/www/contato) add .php and make sure it’s a file -f (file)
  • If true apply the rule below
  • !.*\.php$ %{REQUEST_FILENAME}.php - If you haven’t .php at the end, add, keeping the settings of Querystring [QSA].

Even if the rule is to add .php in the end this is an internal configuration, for PHP to identify the file, the user does not even know it occurs.

  • 3

    Let me explain what it does?

  • Perfect Pedro Morais. It worked correctly. Thank you!

  • 1

    Of course, if you want to supplement the answer feel free =)

  • 1

    If the explanation is not clear, warn me to improve

  • Very good explanation William. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.