Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的搜索引擎优化友好且干净的URL_Php_Database_Wordpress_Url_Url Rewriting - Fatal编程技术网

php中的搜索引擎优化友好且干净的URL

php中的搜索引擎优化友好且干净的URL,php,database,wordpress,url,url-rewriting,Php,Database,Wordpress,Url,Url Rewriting,其实我的问题不能直接理解,但我想你们可以从下面的例子中理解 mashable.com/2012/12/11/responsive-web-design/ 在wordpress中,服务器中没有任何html或php页面或文件夹名称小工具,但当用户访问此链接时,将以html形式打开 我创建了我自己的网站,用户登录后,他们就可以访问我自己的网站 www.website.com/profile.php,但我想让每个用户都像这样链接 www.website.com/userid,如facebook

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

  • mashable.com/2012/12/11/responsive-web-design/
在wordpress中,服务器中没有任何html或php页面或文件夹名称小工具,但当用户访问此链接时,将以html形式打开

我创建了我自己的网站,用户登录后,他们就可以访问我自己的网站 www.website.com/profile.php,但我想让每个用户都像这样链接 www.website.com/userid,如facebook


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


感谢您阅读并回答

启用mod_rewrite,apache=>htaccess

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

我想你是在问关于干净URL的问题吧

您不想这样做
http://mysite.com/profile.php?id=foo

但是您想要这个
http://mysite.com/foo

在这里,您将用户名(在本例中为“foo”)传递给某个脚本,以便您可以使用它做一些事情

@godesign告诉您需要在.htaccess文件中启用mod_rewrite,这是正确的。你可以用它做很多有趣的事情,你也应该阅读它和正则表达式

这就是我的.htaccess文件的外观(针对示例进行了修改):

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

阅读更多:


在谷歌搜索了这个问题并找到了解决方案后,我在这个链接上写道

0)问题

我试着这样问你:

// 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);
}
我想打开类似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;
}

我想你正在寻找
.htaccess->mod_rewrite
只要用谷歌搜索它,你就会找到你需要的。我知道关于.htaccess的一切,我也用它来重写和压缩gzip,但我的问题是不同的。我想得到像www.website.com/userid这样的url,许多用户从一个php文件中获得其配置文件信息profile.php不象www.website.com/profile.php所有用户的固定url不我不想把它重写成特定的固定url,但我想得到像www.website.com/userid这样的url,许多用户从一个php文件profile.php获得其配置文件信息不象www.website.com/profile.php所有用户的固定url不我不需要它,我只举一个例子wordpress和facebook的链接是有效的,正如我所说,我解释我需要的是在我对hangover的回复的评论中。谢谢,但我检查了我的wordpress.htaccess文件,但它没有像这样的重写规则。如果任何用户直接写入其配置文件url,如www.website.com/username,则我们无法从数据库获取数据,因为它需要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);
}