How to create dynamic/friendly URLs using PHP?

[亡魂溺海] 提交于 2019-12-08 06:46:20

问题


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.


回答1:


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");
}
?>



回答2:


http://articles.sitepoint.com/article/search-engine-friendly-urls

http://www.alistapart.com/articles/succeed/

Search revealed dozens more




回答3:


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. e.g. 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




回答4:


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/



来源:https://stackoverflow.com/questions/3410134/how-to-create-dynamic-friendly-urls-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!