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?

5
  • Look at the mod_rewrite component of apache. You can have a rewrite rule saying that for all the urls without no .something to rewrite them with lets say .php. Commented Apr 9, 2013 at 1:11
  • 1
    use htaccess file of apache (read or rewrite rules) Commented Apr 9, 2013 at 1:11
  • awesome thanks I will check those out. It has been annoying me for months Commented Apr 9, 2013 at 1:12
  • If you name then index.php you can just go to website.com Commented Apr 9, 2013 at 1:24
  • There are many answers on stack overflow about this just search how do i remove .php from my files in google Commented Apr 9, 2013 at 1:25

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.