0

I have a build a few sites for my website. They all end with .php.

The problem is if you want to view the page you have to type in website.com/page.php instead of website.com/page. How do I make this happen for all of my main pages?

Is there a quick way of doing this, or do you have to set up a forwarding for all of the /pages to the /page.php?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim Cooley
  • 749
  • 4
  • 19
  • 38

2 Answers2

2

In most cases this is achieved using MVC framework and Routing. It works in a way that you don't access single .php file for single web page you show to user. Every request goes through one file and you have a router where you define your routes and then define what action controller would that route invoke, and from there you choose what view file will you show to the user. Its hard to explain in few sentances. Anyway using MVC you get nice URL-s like www.example.com/controller/action/param

Now if you just want to remove .php extension from your files you can put this in your .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

But other then hiding .php, it wont do any good.

The best thing you can do is to read about MVC frameworks, Routing and Front Controller pattern, and take it from there, its not all about nice URL-s, there's much more to gain! And if you just want to hide .php extension then use above code.

Hope this helps!

Matija
  • 2,610
  • 1
  • 18
  • 18
0

You save your first file as index.php (index.php is the default page) and include or redirect all the other files internally. So there would be no reason to type a file name. You can also use apache on .htaccess to rewrite your files, but you have to be careful with this.

Nik Drosakis
  • 2,258
  • 21
  • 30