简体   繁体   English

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

[英]SEO friendly and clean URL in php

Actually my question can not understand directly but i think you can understand from following example 其实我的问题不能直接理解,但我认为你可以从下面的例子中理解

here in wordpress not any html or php page page or folder name gadgets is not available in server but when user visit this link open as html 在wordpress中没有任何html或php页面或文件夹名称小工具在服务器中不可用但是当用户访问此链接时打开为html

i create my own website where uer login they visit there profile from www.website.com/profile.php but i want to give every user to link like www.website.com/userid like facebook 我创建了自己的网站,在那里你登录他们访问www.website.com/profile.php的个人资料,但我想给每个用户链接像www.website.com/userid像facebook


so i want to know only how to these url are open i develop whole website but i want update only /profile.php to /username 所以我想知道如何这些网址是开放的我开发整个网站,但我想只更新/profile.php到/用户名

thanks for read it and answer it 谢谢你阅读并回答它

启用mod_rewrite,apache => htaccess

on this link i write after googling about this problem and i found solution Click here for see this tutorial then read following steps 在这个链接上我写了一篇关于这个问题的谷歌搜索后,我找到了解决方案点击这里查看本教程,然后阅读以下步骤

0) Question 0)问题

I try to ask you like this : 我试着问你这样:

i want to open page like facebook profile www.facebook.com/kaila.piyush 我想打开像facebook profile www.facebook.com/kaila.piyush这样的页面

it get id from url and parse it to profile.php file and return featch data from database and show user to his profile 它从url获取id并将其解析为profile.php文件并从数据库返回featch数据并将用户显示给他的个人资料

normally when we develope any website its link look like www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678 通常当我们开发任何网站时,其链接看起来像www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678

now we update with new style not rewrite we use www.website.com/username or example.com/weblog/2000/11/23/5678 as permalink 现在我们更新新风格而不是重写我们使用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 1).htaccess

Create a .htaccess file in the root folder or update the existing one : 在根文件夹中创建.htaccess文件或更新现有文件:

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

What does that do ? 那是做什么的?

If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php. 如果请求是针对真实目录或文件(服务器上存在的那个),则不提供index.php,否则每个url都会重定向到index.php。

2) index.php 2)index.php

Now, we want to know what action to trigger, so we need to read the URL : 现在,我们想知道要触发的操作,因此我们需要读取URL:

In index.php : 在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 2)profile.php

Now in the profile.php file, we should have something like this : 现在在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);
}

If you want a WordPress site where each user has their own subsite you should look at BuddyPress . 如果你想要一个WordPress网站,每个用户都有自己的子网站,你应该看看BuddyPress It's a WordPress social network plugin. 这是一个WordPress社交网络插件。

I think you are asking about clean urls? 我想你问的是干净的网址?

You don't want to do this http://mysite.com/profile.php?id=foo 你不想这样做http://mysite.com/profile.php?id=foo

But you would like this http://mysite.com/foo 但你想要这个http://mysite.com/foo

Where you pass the username, in this case 'foo' to some script so you can do something with it. 你传递用户名的地方,在这种情况下'foo'到某个脚本,所以你可以用它做点什么。

@godesign got it right by telling you that you need to enable mod_rewrite in your .htaccess file. @godesign告诉您需要在.htaccess文件中启用mod_rewrite。 There are a lot of interesting things you can do with it, and you should read up on it and regular expressions too. 你可以用它做很多有趣的事情,你应该阅读它和正则表达式。

This is what my .htaccess file looks like (modified for the example): 这是我的.htaccess文件的样子(为示例修改):

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

This takes whatever matches the regular expression - the ^([az]+)$ bit - and puts it into the $1 variable. 这采用正则表达式的任何匹配 - ^([az]+)$位 - 并将其放入$1变量中。

Read more: 阅读更多:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM