Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

clean urls - Remove .php extension with PHP

I could use some help here. I'm using this to fix my URL but I can't figure out how to remove the .php extension. The URL looks like this now: http://mydomain.com/page.php/foo/123/bar/456

function decode_URL_parameters() {
   $path = @$_SERVER['PATH_INFO'];
   $url_array=explode('/',$path);

   array_shift($url_array);

   while ($url_array) {
      $_GET[$url_array[0]] = $url_array[1];
      array_shift($url_array);
      array_shift($url_array);
   }
}

Any ideas?

/Tobias

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're serving via Apache, you'll want to look at mod_rewrite.

Using mod_rewrite, you can modify how URLs are mapped to your application's actual end-points. For your example, you'll want something like this:

RewriteEngine on 
RewriteRule ^/?page/(.*)$ page.php/$1 [L]

This page has some other simple examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...