簡體   English   中英

在php中的SEO友好和干凈的URL

[英]SEO friendly and clean URL in php

其實我的問題不能直接理解,但我認為你可以從下面的例子中理解

在wordpress中沒有任何html或php頁面或文件夾名稱小工具在服務器中不可用但是當用戶訪問此鏈接時打開為html

我創建了自己的網站,在那里你登錄他們訪問www.website.com/profile.php的個人資料,但我想給每個用戶鏈接像www.website.com/userid像facebook


所以我想知道如何這些網址是開放的我開發整個網站,但我想只更新/profile.php到/用戶名

謝謝你閱讀並回答它

啟用mod_rewrite,apache => htaccess

在這個鏈接上我寫了一篇關於這個問題的谷歌搜索后,我找到了解決方案點擊這里查看本教程,然后閱讀以下步驟

0)問題

我試着問你這樣:

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

如果你想要一個WordPress網站,每個用戶都有自己的子網站,你應該看看BuddyPress 這是一個WordPress社交網絡插件。

我想你問的是干凈的網址?

你不想這樣做http://mysite.com/profile.php?id=foo

但你想要這個http://mysite.com/foo

你傳遞用戶名的地方,在這種情況下'foo'到某個腳本,所以你可以用它做點什么。

@godesign告訴您需要在.htaccess文件中啟用mod_rewrite。 你可以用它做很多有趣的事情,你應該閱讀它和正則表達式。

這是我的.htaccess文件的樣子(為示例修改):

RewriteEngine On
RewriteRule ^([a-z]+)$ profile.php?id=$1 [L]

這采用正則表達式的任何匹配 - ^([az]+)$位 - 並將其放入$1變量中。

閱讀更多:

http://wettone.com/code/clean-urls

http://corz.org/serv/tricks/htaccess2.php

http://www.branded3.com/blogs/htaccess-mod_rewrite-ultimate-guide/

暫無
暫無

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

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