创建类似于文件夹结构的博客文章链接 [英] Create blog post links similar to a folder structure

查看:87
本文介绍了创建类似于文件夹结构的博客文章链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个博客,我想以下面的形式创建链接到我的个人文章:

  http://www.mysite.com/health/2013/08/25/some-random-title 
------ -----------------
| |
类别标题

但是我不知道如何实现这一点。



我发现了一些会给我URI的东西。

  $ uri = $ _SERVER [REQUEST_URI]; 

然后,我将继续提取所需的部分并针对数据库发出请求。
这似乎是一个非常非常愚蠢的问题,但我不知道如何在谷歌上查看(我试过......),但我要如何处理链接?



我试着一步一步解释它:



用户点击文章标题 - >页面重新加载新的uri - >我应该在哪里处理这个新的uri,以及如何?如果请求路径如下所示:



index.php?title = some-random-article-title



我会在index.php中执行该操作,并读取$ _GET数组并相应地处理它。但是,在这个问题的开始,我该如何处理这个建议的结构呢?

解决方案

您需要做以下几件事:


  1. 设置.htaccess将所有请求重定向到您的主文件,该文件将处理所有这些内容,例如:

     < IfModule mod_rewrite.c> 
    RewriteEngine On
    RewriteBase /
    RewriteRule ^ index\.php $ - [L]
    RewriteCond%{REQUEST_FILENAME}!-f
    RewriteCond%{REQUEST_FILENAME}! -d
    RewriteRule。 /index.php [L]
    < / IfModule>

    以上将重定向不存在的文件和文件夹的所有请求到 index.php


  2. 现在您要处理URL路径,以便您可以使用PHP变量 $ _ SERVER ['REQUEST_URI'] ,正如你所提到的那样。 它可以提取你想要的信息,你可以使用 parse_url pathinfo 爆炸,这样做。

  3. 使用

      $ s =空($ _ SERVER 这可能是最明显的方法: [HTTPS])? '':($ _SERVER [HTTPS] ==on)? https:http; 
    $ url = $ s。 '://'。 $ _SERVER [HTTP_HOST]。 $ _SERVER [ REQUEST_URI];
    var_dump(parse_url($ url));

    输出:

      [scheme] =>字符串(4)http
    [host] =>字符串(10)domain.com
    [path] =>字符串(36)/ health / 2013/08/25 / some-random-title
    [query] =>字符串(17)with = query-string

    所以 parse_url <
    $ b

    例如使用 pathinfo :code>可以很容易地分解当前的URL。

      $ path_parts = pathinfo($ _ SERVER ['REQUEST_URI']); 

    $ path_parts ['dirname'] 会返回 / health / 2013/08/25 /


    $ b

    $ path_parts ['basename' ] 会返回 some-random-title ,如果它有一个扩展名,它会返回 some-random-title.html



    $ path_parts ['extension'] 会返回空,如果它有它会返回 .html



    $ path_parts ['filename'] 会返回 some-random-title ,如果它有一个扩展名,它会返回 some-random-title.html

    $ b

    使用爆炸这样的事情:

      $ parts = explode('/',$ path); 
    foreach($ parts as $ part)
    echo $ part,\\\
    ;

    输出:

      health 
    2013
    08
    25
    some-random-title.php

    当然,这些只是您如何阅读它的例子。



    您也可以使用.htaccess制定特定规则例如:

      RewriteRule ^([^ /] +)/([0-9] +)/([0-9] +)/([0-9] +)/([^ /] +)/?$ blog.php?category = $ 1& date = $ 2- $ 3- $ 4& title = $ 5 [L] 

    基本上,上述内容会分解URL路径并将其内部重定向到您的文件blog.php以及适当的参数,所以使用你的URL示例它会重定向到:

      http://www.mysite。 com / blog.php?category = health& date = 2013-08-25& title = some-random-title 

    然而,在客户端浏览器中,URL将保持不变:

      http://www.mysite.com/健康/ 2013/08/25 /一些随机悌tle 

    还有其他一些功能可能适用于此,例如 parse_url pathinfo

    I am currently working on a blog where I would like to create links to my individual articles in the following form:

    http://www.mysite.com/health/2013/08/25/some-random-title
                          ------            -----------------
                            |                       |
                         category                 title
    

    However I have no idea how to achieve this.

    I have found something that would give me the URI.

    $uri = $_SERVER["REQUEST_URI"];
    

    I would then go ahead and extract the needed parts and make requests against the database. This may seem a very very dumb question, but I do not know how to look this up on google (I tried...) but how exactly am I going to handle the link ?

    I try to explain it step-by-step:

    User clicks on article title -> the page reloads with new uri --> Where am I supposed to handle this new uri and how ? If the request path looked like this:

    index.php?title=some-random-article-title

    I would do it in the index.php and read the $_GET array and process it accordingly. But how do I do it with the proposed structure at the beginning of this question ?

    解决方案

    You will need a few things:

    1. Setup an .htaccess to redirect all request to your main file which will handle all that, something like:

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      

      The above will redirect all request of non-existent files and folder to your index.php

    2. Now you want to handle the URL Path so you can use the PHP variable $_SERVER['REQUEST_URI'] as you have mentioned.

    3. From there is pretty much parse the result of it to extract the information you want, you could use one of the functions parse_url or pathinfo or explode, to do so.

    Using parse_url which is probably the most indicated way of doing this:

    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "https" : "http";
    $url = $s . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    var_dump(parse_url($url));
    

    Output:

    ["scheme"] => string(4) "http" 
    ["host"]   => string(10) "domain.com" 
    ["path"]   => string(36) "/health/2013/08/25/some-random-title" 
    ["query"]  => string(17) "with=query-string"
    

    So parse_url can easily break down the current URL as you can see.

    For example using pathinfo:

    $path_parts = pathinfo($_SERVER['REQUEST_URI']);
    

    $path_parts['dirname'] would return /health/2013/08/25/

    $path_parts['basename'] would return some-random-title and if it had an extension it would return some-random-title.html

    $path_parts['extension'] would return empty and if it had an extension it would return .html

    $path_parts['filename'] would return some-random-title and if it had an extension it would return some-random-title.html

    Using explode something like this:

    $parts = explode('/', $path);
    foreach ($parts as $part)
        echo $part, "\n";
    

    Output:

    health
    2013
    08
    25
    some-random-title.php
    

    Of course these are just examples of how you could read it.

    You could also use .htaccess to make specific rules instead of handling everything from one file, for example:

    RewriteRule ^([^/]+)/([0-9]+)/([0-9]+)/([0-9]+)/([^/]+)/?$ blog.php?category=$1&date=$2-$3-$4&title=$5 [L]
    

    Basically the above would break down the URL path and internally redirect it to your file blog.php with the proper parameters, so using your URL sample it would redirect to:

    http://www.mysite.com/blog.php?category=health&date=2013-08-25&title=some-random-title
    

    However on the client browser the URL would remain the same:

    http://www.mysite.com/health/2013/08/25/some-random-title
    

    There are also other functions that might come handy into this for example parse_url, pathinfo like I have mentioned early, server variables, etc...

    这篇关于创建类似于文件夹结构的博客文章链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆