簡體   English   中英

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

[英]How to create friendly URL in php?

通常,顯示某些個人資料頁面的做法或非常古老的方式是這樣的:

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

其中u=12345是用戶 ID。

近年來,我發現一些網站的網址非常好,例如:

www.domain.com/profile/12345

如何在 PHP 中執行此操作?

就像一個瘋狂的猜測,它與.htaccess文件有關嗎? 你能給我更多關於如何編寫.htaccess文件的提示或一些示例代碼嗎?

根據這篇文章,您需要一個看起來像這樣的 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'
?>

我最近在一個非常適合我的需求的應用程序中使用了以下內容。

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

索引.php

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

我嘗試在以下示例中逐步解釋此問題。

0) 問題

我試着這樣問你:

我想打開像 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;
}

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

做到這一點的簡單方法。 試試這個代碼。 將代碼放入您的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

它實際上不是 PHP,它是使用 mod_rewrite 的 apache。 發生的情況是該人請求鏈接 www.example.com/profile/12345,然后 apache 使用重寫規則將其切碎,使其看起來像這樣,www.example.com/profile.php?u=12345,到服務器。 您可以在此處找到更多信息:重寫指南

ModRewrite 不是唯一的答案。 您還可以在 .htaccess 中使用 Options +MultiViews,然后檢查$_SERVER REQUEST_URI以查找 URL 中的所有內容。

有很多不同的方法可以做到這一點。 一種方法是使用前面提到的 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']));

從那里,您可以隨心所欲地解析它,但為了皮特的緣故,請確保對其進行消毒;)

看起來您在談論 RESTful Web 服務。

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

.htaccess 文件確實重寫了所有 URI 以指向一個控制器,但這比您此時想要獲得的要詳細得多。 你可能想看看Recess

這是一個 RESTful 框架,全部使用 PHP

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM