如何在php中创建友好的URL?

如何在php中创建友好的URL?,php,url,friendly-url,Php,Url,Friendly Url,通常,显示某些配置文件页面的做法或非常古老的方式如下: www.domain.com/profile.php?u=12345 RewriteEngine on RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1 其中u=12345是用户id 近年来,我发现一些网站有非常好的URL,如: www.domain.com/profile/12345 如何在PHP中实现这一点 正如胡乱猜测的那样,它是否与.htaccess文件有关?关于如何

通常,显示某些配置文件页面的做法或非常古老的方式如下:

www.domain.com/profile.php?u=12345
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
其中
u=12345
是用户id

近年来,我发现一些网站有非常好的URL,如:

www.domain.com/profile/12345
如何在PHP中实现这一点

正如胡乱猜测的那样,它是否与
.htaccess
文件有关?关于如何编写
.htaccess
文件,您能给我更多提示或一些示例代码吗?

根据,您需要一个类似以下内容的mod_rewrite(放置在
.htaccess
文件中)规则:

www.domain.com/profile.php?u=12345
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
这映射了来自

/news.php?news_id=63

另一种可能是使用
forcetype
执行,这会强制任何特定路径的内容使用php来评估内容。因此,在
.htaccess
文件中,放入以下内容:

<Files news>
    ForceType application/x-httpd-php
</Files>

它实际上不是PHP,而是使用mod_重写的apache。发生的情况是,此人请求链接www.example.com/profile/12345,然后apache使用重写规则将其切碎,使其看起来像服务器上的链接www.example.com/profile.php?u=12345。您可以在这里找到更多信息:

看起来您正在谈论一个RESTful Web服务

htaccess文件确实重写了所有URI以指向一个控制器,但这比您现在想要得到的更详细。你可能想看看


这是一个完全用PHP编写的RESTful框架

我最近在一个应用程序中使用了以下内容,该应用程序可以很好地满足我的需要

.htaccess

<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On

# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>

ModRewrite不是唯一的答案。您还可以在.htaccess中使用选项+多视图,然后检查
$\u SERVER REQUEST\u URI
以查找URL中的所有内容。

有很多不同的方法可以做到这一点。一种方法是使用前面提到的重写规则技术来屏蔽查询字符串值

我非常喜欢的一种方式是,如果使用该模式,还可以使用类似URL的URL,并解析$\u SERVER['REQUEST\u URI']的值

您可以使用以下代码位轻松提取/path/to/your/page/here位:

$route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));

从那里,你可以随心所欲地解析它,但看在皮特的份上,一定要对它进行消毒;)

我试图在下面的例子中一步一步地解释这个问题

0)问题

我试着这样问你:

// profile.php

function profile($chars)
{
    // We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)

    if (is_int($chars)) {
        $id = $chars;
        // Do the SQL to get the $user from his ID
        // ........
    } else {
        $username = mysqli_real_escape_string($char);
        // Do the SQL to get the $user from his username
        // ...........
    }

    // Render your view with the $user variable
    // .........
}

function myProfile()
{
    // Get the currently logged-in user ID from the session :
    $id = ....

    // Run the above function :
    profile($id);
}
我想打开类似facebook个人资料的页面www.facebook.com/kaila.piyush

它从url获取id并将其解析为profile.php文件,然后从数据库返回featch数据,并将用户显示到他的概要文件中

通常,当我们开发任何网站时,它的链接都是这样的 www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678

现在我们使用新的样式进行更新,而不是重写。我们使用www.website.com/username或example.com/weblog/2000/11/23/5678作为永久链接

http://example.com/profile/userid (get a profile by the ID) 
http://example.com/profile/username (get a profile by the username) 
http://example.com/myprofile (get the profile of the currently logged-in user)
1).htaccess

在根文件夹中创建.htaccess文件或更新现有文件:

Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
那有什么用

如果请求是一个真实的目录或文件(存在于服务器上),则不会提供index.php,否则每个url都会重定向到index.php

2)index.php

现在,我们想知道要触发什么操作,因此需要读取URL:

在index.php中:

// index.php    

// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);

for ($i= 0; $i < sizeof($scriptName); $i++)
{
    if ($requestURI[$i] == $scriptName[$i])
    {
        unset($requestURI[$i]);
    }
}

$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :

$command = array(
    [0] => 'profile',
    [1] => 19837,
    [2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :

// index.php

require_once("profile.php"); // We need this file
switch($command[0])
{
    case ‘profile’ :
        // We run the profile function from the profile.php file.
        profile($command([1]);
        break;
    case ‘myprofile’ :
        // We run the myProfile function from the profile.php file.
        myProfile();
        break;
    default:
        // Wrong page ! You could also redirect to your custom 404 page.
        echo "404 Error : wrong page.";
        break;
}

这是一个简单的方法。试试这个代码。将代码放入
htaccess
文件:

Options +FollowSymLinks

RewriteEngine on

RewriteRule profile/(.*)/ profile.php?u=$1

RewriteRule profile/(.*) profile.php?u=$1   
它将创建以下类型的URL:


有关详细信息,请参阅htaccess Pretty URL:

一个很棒的mod_rewrite资源:如果您想完全用PHP重写URL,另一个选项是设置重写规则,将所有请求定向到单个脚本。这样做的一大好处是,您可以将代码存储在Apache无法访问的区域中,并且只有URL调度脚本需要Apache可以访问。e、 g:RewriteCond%{REQUEST_FILENAME}-f RewriteCond%{REQUEST_FILENAME}-重写规则。php这与您的forcetype示例类似。关于Phpriot的文章已被删除。如果id=1,post_name=“red post”,那么是否有可能创建类似example.com/red-post.php的url。目前我正在创建类似example.com/post/?id=1的url。ie如何通过帖子名制作slug(url)
James Socol
Chad Birch
发布的链接已断开。是的,但多视图很讨厌。。。我得说,这个用法太笼统了。从技术上说,我想你是对的,但除非OP已经体验过REST的想法,否则即使阅读Wikipedia的文章也远远超出了问题的需要。在这种情况下,调用一个完整的编程范例是过分的。真的吗?我不认为他的问题完全是“我如何制作profile.php?u=12345 goto/profile/12345”,我将其解释为“在生产环境中如何实现?”。我认为知道有一个范例是有益的,当有适当的方法解决这个问题时,没有一个满是重定向的.htaccess文件。感谢这篇详细的文章,我一直想学习,像wordpress这样的网站如何生成漂亮的URL,并且可以在每次不编辑htaccess文件的情况下访问它们,谢谢这正是我想要的。你如何处理锚?简单而优雅。为我工作。
Options +FollowSymLinks

RewriteEngine on

RewriteRule profile/(.*)/ profile.php?u=$1

RewriteRule profile/(.*) profile.php?u=$1