如何创建和使用假目录结构?

发布于 2024-07-29 00:38:32 字数 1429 浏览 2 评论 0原文

我一直在查看一些假装 URL 中有目录结构的网站,并且想知道“如何实现?”。

我正在工作中控制一个网站并查看了代码。 他们有一个所有页面的数据库,并且是动态创建的。

我可以让主页在本地服务器上运行,但我不知道从哪里开始使用假目录结构。 一个例子是http://www.bankcharges.com/bank-charges-advice/ - 没有目录,但内容在数据库中。

他们是如何做到这一点的?

我认为与之相关的代码是:

index.php:

<?php

    include('includes/functions.php');

    $activeTab = "navhome"; 
    $sent = false;

    $title = (isset($_GET['title']))? mysql_real_escape_string($_GET['title']) : 'Home';    
    $title = str_replace('-',' ', $title);

    if($title != '') {  

        $sql = "SELECT * 
                FROM contents 
                WHERE name LIKE '%$title%'
                LIMIT 1";

        $result = @mysql_query($sql);       
        $row = mysql_fetch_assoc($result);      
    }

    //Set page title
    $pagetitle = (isset($row['name']) && $title != 'Home')? ucwords($row['name']) : "Bank Charges";
?>

functions.php:

<?php

include('database.php');
include('settings.php');

//Nice URL's
function url($str){
$arr = array('!','"','£','$','%','^','&','*','(',')','_','+','{','}',':','@','~','<','>','?','|',',','.','\\','/',';',']','[','\'');    
$str = str_replace($arr,"", str_replace(" ","-",strtolower($str))); 
return $str;        
}

function isEven($v){
    if($v % 2 == 0) return true;        
}

?>

I have been looking at some sites that pretend that there is a directory structure in the URL and wondered 'how?'.

I am taking control of a website at work and have looked over the code. They have a database for all the pages and they are created dynamically.

I can get the homepage working on my local server but I don't have a clue as to where to start with the fake directory structure. An example is http://www.bankcharges.com/bank-charges-advice/ - there is no directory for this but the content is in the database.

How have they done this?

The code this I think is related to is:

index.php:

<?php

    include('includes/functions.php');

    $activeTab = "navhome"; 
    $sent = false;

    $title = (isset($_GET['title']))? mysql_real_escape_string($_GET['title']) : 'Home';    
    $title = str_replace('-',' ', $title);

    if($title != '') {  

        $sql = "SELECT * 
                FROM contents 
                WHERE name LIKE '%$title%'
                LIMIT 1";

        $result = @mysql_query($sql);       
        $row = mysql_fetch_assoc($result);      
    }

    //Set page title
    $pagetitle = (isset($row['name']) && $title != 'Home')? ucwords($row['name']) : "Bank Charges";
?>

functions.php:

<?php

include('database.php');
include('settings.php');

//Nice URL's
function url($str){
$arr = array('!','"','£','
,'%','^','&','*','(',')','_','+','{','}',':','@','~','<','>','?','|',',','.','\\','/',';',']','[','\'');    
$str = str_replace($arr,"", str_replace(" ","-",strtolower($str))); 
return $str;        
}

function isEven($v){
    if($v % 2 == 0) return true;        
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抠脚大汉 2024-08-05 00:38:32

mod_rewrite 是允许这种情况发生的 apache 模块。 其他网络服务器有自己的实现。

如需初学者指南,请查看此博文

mod_rewrite is the apache module that allows this to occur. Other web servers have their own implementations.

For a beginners guide, check out this blog post.

雪化雨蝶 2024-08-05 00:38:32

如果您不知道从哪里开始,可能会有点复杂...您必须使用类似这样的内容重写 URL

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?url=$0 [L,QSA]

然后当您访问类似 http://www.bankcharges.com/bank 的 URL 时-charges-advice/,您的服务器实际上会调用http://www.bankcharges.com/index.php?url=bank-charges-advice/。 然后在 index.php 中,您可以根据需要处理该查询(通常通过从数据库中提取某些内容)。 从学习 mod_rewrite 开始。

Might be a bit complicated if you don't know where to begin... you have to rewrite the URLs with something like this

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?url=$0 [L,QSA]

And then when you go to a URL like http://www.bankcharges.com/bank-charges-advice/, your server will actually call up http://www.bankcharges.com/index.php?url=bank-charges-advice/. And then in index.php you can handle that query however you want (usually by pulling something from the database). Start by learning mod_rewrite.

千柳 2024-08-05 00:38:32

在谷歌上搜索 url rewrite。

这是 来自 apache 的指南

search for url rewrite on google.

here is a guide from apache

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文