How to create friendly URL in php?

问题描述 投票:0回答:8

通常,一些个人资料页面的做法或非常古老的方式是这样的:

www.domain.com/profile.php?u=12345

其中

u=12345
是用户ID。

近几年,我发现了一些网址非常漂亮的网站,例如:

www.domain.com/profile/12345

如何在 PHP 中执行此操作?

胡乱猜测,是不是跟

.htaccess
文件有关?你能给我更多关于如何编写
.htaccess
文件的提示或示例代码吗?

php url friendly-url
8个回答
52
投票

根据this article,你想要一个看起来像这样的mod_rewrite(放置在

.htaccess
文件中)规则:

RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

这映射了来自

的请求
/news.php?news_id=63

/news/63.html

另一种可能性是使用

forcetype
来实现,它会强制任何沿着特定路径的内容使用 php 来评估内容。所以,在你的
.htaccess
文件中,放入以下内容:

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

然后 index.php 可以根据

$_SERVER['PATH_INFO']
变量采取行动:

<?php
    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'
?>

31
投票

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

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

index.php

foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
    // Figure out what you want to do with the URL parts.
}

15
投票

我试着在下面的例子中逐步解释这个问题。

0) 问题

我试着这样问你:

我想打开像 facebook 个人资料这样的页面 www.facebook.com/kaila.piyush

它从 url 获取 id 并将其解析为 profile.php 文件并从数据库返回 featch 数据并将用户显示到他的个人资料

通常当我们开发任何网站时,它的链接看起来像 www.website.com/profile.php?id=用户名 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;
}

2) profile.php

现在在 profile.php 文件中,我们应该有这样的东西:

// 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);
}

10
投票

简单的方法来做到这一点。试试这段代码。将代码放入您的

htaccess
文件中:

Options +FollowSymLinks

RewriteEngine on

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

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

它将创建这种类型的漂亮 URL:

http://www.domain.com/profile/12345/

更多htaccess漂亮的网址:http://www.webconfs.com/url-rewriting-tool.php


4
投票

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


-1
投票

ModRewrite 不是唯一的答案。您还可以在 .htaccess 中使用 Options +MultiViews,然后检查

$_SERVER REQUEST_URI
以查找 URL 中的所有内容。


-1
投票

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

我真正喜欢的方法之一是如果你使用前端控制器模式,你也可以使用像http://yoursite.com/index.php/path/to/your/page/here这样的url并解析$_SERVER['REQUEST_URI'] 的值。

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

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

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


-2
投票

看起来你在谈论 RESTful 网络服务。

http://en.wikipedia.org/wiki/Representational_State_Transfer

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

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

© www.soinside.com 2019 - 2024. All rights reserved.