Forum Moderators: coopster

Message Too Old, No Replies

Want to remove file extensions from the URL -- possible?

by using PHP

         

londrum

9:41 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've got one of those lousy hosts which don't allow you to use a .htaccess file. so if i want to rewrite a URL i have to do it all in php.

i've got this piece of code working already, which redirects

www.example.com/index.html
to
www.example.com/

function fix_index_url(){
if(preg_match('#(.*)index\.(html¦php)$#',$_SERVER['REQUEST_URI'],$captures)){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$captures[1]);}
;}
fix_index_url();

(remember to replace the broken pipe!)

but now i'm trying to work out something that will get rid of all the file extensions as well. for example...

www.example.com/page.html
would redirect to
www.example.com/page

i know i could use something similar to the piece of code above, but for the life of me, i can't work out the preg_match expression to use. anyone help a poor guy out?

ratman7

11:25 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



How about switching web hosts? Not trying to discourage you -- but with all of the cheap hosting out there, it's probably not worth trying to come up with a workaround when .htaccess is the proper way to accomplish what you're looking to do.

coopster

2:07 am on Jan 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tested the extensionless URI first in your browser? I mean, open a browser and see if

www.example.com/page

is going to serve the

www.example.com/page.html

page. If your host is so tight that mod_rewrite is disabled, I highly doubt you are going to be able to serve anything via content negotiation!