代码之家  ›  专栏  ›  技术社区  ›  Nimsara Madhubashini

通过PHP、MySQL和htaccess创建自定义url[duplicate]

  •  0
  • Nimsara Madhubashini  · 技术社区  · 7 年前

    通常,显示某个配置文件页面的做法或非常古老的方式如下:

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

    u=12345 是用户id。

    最近几年,我发现了一些网址很好的网站,比如:

    www.domain.com/profile/12345
    

    如何在PHP中实现这一点?

    就像一个疯狂的猜测,这和 .htaccess 文件?你能给我更多的提示或者一些关于如何编写 .htaccess接口 文件?

    0 回复  |  直到 12 年前
        1
  •  45
  •   Chris Forrence Shreya Gupta    9 年前

    根据 this article ,您需要一个mod_重写(放置在 .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>
    

    $_SERVER['PATH_INFO'] 变量:

    <?php
        echo $_SERVER['PATH_INFO'];
        // outputs '/63.html'
    ?>
    
        2
  •  29
  •   Jordan S. Jones    16 年前

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

    index.php索引

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

    我试图在下面的例子中一步一步地解释这个问题。

    0)问题

    我想打开像facebook profile这样的页面www.facebook.com/kaila.piyush

    它从url获取id并将其解析为profile.php文件,然后从数据库返回featch数据并将用户显示到其profile

    www.website.com/profile.php?id=用户名 example.com/weblog/index.php?y=2000&m=11&d=23&id=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。

    现在,我们想知道要触发什么操作,所以需要读取URL:

    // 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
    
    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);
    }
    
        4
  •  9
  •   Hemal Halari    9 年前

    简单的方法。试试这个代码。将代码放入 htaccess 文件:

    Options +FollowSymLinks
    
    RewriteEngine on
    
    RewriteRule profile/(.*)/ profile.php?u=$1
    
    RewriteRule profile/(.*) profile.php?u=$1   
    

    它将创建这种类型的漂亮URL:

    http://www.domain.com/profile/12345/

    http://www.webconfs.com/url-rewriting-tool.php

        5
  •  4
  •   jacobangel    16 年前

    它实际上不是PHP,而是使用mod_rewrite的apache。会发生什么情况:此人请求链接www.example.com/profile/12345,然后apache使用重写规则将其切碎,使其看起来像这样,www.example.com/profile.php?u=12345,到服务器。你可以在这里找到更多: Rewrite Guide

        6
  •  0
  •   empi    16 年前

    $_SERVER REQUEST_URI 查找URL中的所有内容。

        7
  •  0
  •   Shabbyrobe    16 年前

    有很多不同的方法可以做到这一点。一种方法是使用前面提到的重写规则技术来屏蔽查询字符串值。

    front controller 模式,您还可以使用如下URL http://yoursite.com/index.php/path/to/your/page/here 并分析$_SERVER['REQUEST_URI']的值。

    您可以使用以下代码轻松提取/path/to/your/page/here位:

    $route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
    

    从那里,你可以随意解析它,但为了皮特的缘故,请确保你对它进行了消毒;)

        8
  •  -1
  •   ryanday    16 年前

    看起来你在谈论一个RESTful webservice。

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

    Recess

    它是一个RESTful框架,全部使用PHP