-2

I am trying to have my pages accessible with example.com/about, example.com/contacts instead of example.com/about.php & example.com/contact.php

I am not sure how this is done, but I was able to do it by creating link

<a href='site.com/about'> about </a>

And I have my index.php set as

$URI = $_SERVER['REQUEST_URI']; 
switch($URI){
  case '/about';
    include 'about.php';
    break; 
  case '/contact';
    include 'contact.php';
    break; 
  //...
}

Now, asking if this is a good way will have my thread closed as objective question. But I would like to know, how it is done, or more accurately, how everyone does it.

  • Usually via .htaccess URL Rewriting. And no, the switch statement is a bad way of handling this. – Paul Dessert Dec 05 '13 at 23:04
  • But, how? and what is wrong with how I am doing it. I am sure doing it with `.htaccess` will slightly slow down the page speed don't you think? –  Dec 05 '13 at 23:05
  • There are a billion related questions on SO addressing this issue. Search around, you'll find a TON of info – Paul Dessert Dec 05 '13 at 23:06
  • "will slightly slow down the page speed don't you think" --- it would be reasonable to not think about performance for next year or so until you learn some programming practices. And - no, it won't make it slow down noticeable, not only in comparison with your code performance – zerkms Dec 05 '13 at 23:08
  • @zerkms what makes you think learning about Apache re-write equates to basic programming knowledge? –  Dec 05 '13 at 23:22
  • @ANW: the fact that it's trivial perhaps – zerkms Dec 05 '13 at 23:23
  • @zerkms Which should even make it not worth learning. –  Dec 05 '13 at 23:29
  • @ANW: it worth learning, but it doesn't worth thinking about performance when you know that little. Performance optimization **is** a rocket science. You cannot even imagine how complicated it is – zerkms Dec 05 '13 at 23:29

1 Answers1

0

I use this in .htaccess

/blah goes to /blah.php etc. It's not slower than loading a php page, and then loading in there.

RewriteEngine On
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://WEBSITE.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
cooper667
  • 427
  • 4
  • 7