简体   繁体   中英

How to create dynamic/friendly URLs using PHP?

Can anyone explain how to create friendly URLs? I mean URLs like http://store.steampowered.com/app/22600/ that doesn't have any pages like index.php visible.

If you only have cpanel use .htaccess .

If that doesn't work, you are left with parsing the url in php with a link like this:

http://server.com/router.php/search

You can do that with something like this.

<?
list($junk,$url) = explode("router.php",$_SERVER['REQUEST_URI']);
$paths = explode("/", $url);
if ($paths[0] == 'search')
{
   Header("Location: /search.php");
}
?>

You need to look up apache mod_rewrite (assuming you are using apache for your web server). PHP itself doesn't do it for you the web server does most of the work. You need to tell your web server to use mod_rewrite to point all urls that match a certain pattern to point to what ever .php file you like. You can pass arguments in your url pattern what ever way you choose. eg using the / to delimit key values or just values. If you don't have root access to the server and its enabled you can usually put these mod rewrite rules in a .htaccess file at the root of your site.

Sitepoint have a good reference for beginners. http://articles.sitepoint.com/article/guide-url-rewriting

if you're using apache this should work/is the idea:

RewriteRule ^(.*)$ /index.php?/$1 [L]

now your url will go to http://store.steampowered.com/index.php/app/22600/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM