【问题标题】:SEO friendly and clean URL in phpphp中的SEO友好和干净的URL
【发布时间】:2013-03-15 15:16:03
【问题描述】:

其实我的问题不能直接理解,但我想你可以从下面的例子中理解

在 wordpress 中没有任何 html 或 php 页面页面或文件夹名称小工具在服务器中不可用,但当用户访问此链接时以 html 形式打开

我创建了我自己的网站,他们从那里登录用户访问个人资料 www.website.com/profile.php 但我想给每个用户链接 www.website.com/userid 喜欢 facebook


所以我只想知道如何打开这些 url 我开发了整个网站,但我只想更新 /profile.php 到 /username

感谢阅读并回答

【问题讨论】:

  • 我想你正在寻找.htaccess -> mod_rewrite,只要谷歌它,你就会找到你需要的。
  • 我对 .htaccess 了如指掌,我也将它用于重写和 gzip 压缩,但我的问题是 diffrent。我想获得这样的网址 www.website.com/userid 很多用户可以在其中获得它来自一个 php 文件 profile.php 的个人资料信息不像 www.website.com/profile.php 为所有用户修复 url

标签: php database wordpress url url-rewriting


【解决方案1】:

在这个链接上,我在谷歌上搜索了这个问题并找到了解决方案 Click here for see this tutorial then read following steps

0) 问题

我试着这样问你:

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

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

通常当我们开发任何网站时,它的链接看起来像 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);
}

【讨论】:

    【解决方案2】:

    我想你是在问干净的网址?

    你不想这样做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]
    

    这将获取与正则表达式匹配的任何内容 - ^([a-z]+)$ 位 - 并将其放入 $1 变量中。

    阅读更多:

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

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

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

    【讨论】:

    • 谢谢,但我检查了我的 wordpress .htaccess 文件,但它没有任何这样的重写规则。如果任何用户直接写入其个人资料网址,例如 www.website.com/username 我们无法从我们的数据库中获取数据,它需要 profile.php 文件
    【解决方案3】:

    如果您想要一个每个用户都有自己的子网站的 WordPress 网站,您应该查看BuddyPress。这是一个 WordPress 社交网络插件。

    【讨论】:

    • 不,我不需要它,我只举一个 wordpress 和 facebook 的例子,链接是有效的,因为我告诉我在我对宿醉答案的评论中解释我需要什么
    【解决方案4】:

    启用 mod_rewrite, apache => htaccess

    【讨论】:

    • 不,我不想将其重写为特定的修复 url,但我想获得像 www.website.com/userid 这样的 url,其中许多用户从一个 php 文件 profile.php 中获取其个人资料信息,而不喜欢www.website.com/profile.php 修复所有用户的网址
    猜你喜欢
    • 2014-05-19
    • 1970-01-01
    • 2016-04-13
    • 2012-11-09
    • 2012-08-21
    • 2011-05-09
    • 2018-05-22
    • 1970-01-01
    • 2014-10-18
    相关资源
    最近更新 更多