htaccess的重定向与数据库值 [英] Htaccess Redirect with Value from Database

查看:94
本文介绍了htaccess的重定向与数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我已经被今日(帮一次的.htaccess正则表达式不会匹配 ),我跑右转入下一个问题,使我不知道是否我的做法是,在一切可能的。

After I have already been helped once today (Htaccess Regex won't match), I ran right into the next problem, making me wonder if my approach is at all feasible.

我还在努力使用htaccess的改变这种不那么可爱的网址

I'm still trying to use htaccess to change this not so lovely url

http://localhost/test/index.php?page=Article&articleID=61

http://localhost/test/article/2015-09-21-this-is-the-headline

感谢previous问题,我能够制定出正则表达式并转发该字符串的脚本。

Thanks to the previous question I was able to work out the regex and forward the string to a script.

RewriteRule ^article\/(.*)$ article.php?id=$1 [L]

该脚本从数据库获取正确的ID。大多数的回答我通过谷歌发现说,用头去的实际位置。这可是违背了目的,因为它会网址仍然更改为丑。

The script gets the correct id from the database. Most answers I found via google say to use a header to get to the actual location. That however defeats the purpose as it would still change the url to the ugly one.

相反,我试过,包括页面。由于index.php文件需要一定的参数,我去了丑陋的方式和刚才保存的手动操作。

Instead I tried including the page. Since index.php needs certain parameters I went the ugly way and just saved those manually.

<?php
    require_once('config.php');
    require_once(LIB_PATH.'system/DatabaseController.class.php');    
    $dbController = new DatabaseController();

    $id = $dbController->escapeString($_GET['id']);
    $sql = "SELECT articleID FROM ".DATABASE_PREFIX."_article WHERE articleTitleLink = '".$id."'";
    $result = $dbController->getFirstRow($sql);

    if (empty($result)) {
        header('Location: index.php?page=Error404');
        exit();
    } else {
        $_GET['page'] = 'Article';
        $_GET['articleID'] = $result['articleID'];
        include('index.php');
        exit();
    }

这样做的工作出奇地好。它显示正确的页面

Doing this worked surprisingly well. It's displaying the correct page

index.php?page=Article&articleID=61

而呈现出漂亮的网址

while showing the nice url

article/2015-09-21-this-is-the-headline

然而,无论是装CSS的也不是由htaccess的处理的图像。我猜想,因为包括不实际执行PHP。我觉得我要么非常接近成功或完全停留在一个毫无意义的努力。

However, neither is the CSS loaded nor are the images processed by the htaccess. I'm assuming because include doesn't actually execute the php. I feel like I'm either very close to success or completely stuck in a pointless endeavour.

TL; DR:想prettify我的网址。需要从数据来看,这是我通过PHP实现获得一个ID。现在我失去了获得所请求的页面,同时保持了pretty的URL。我的方法的工作或者什么能会是一个更好的办法?

TL;DR: Want to prettify my urls. Requires getting an id from the data, which I achieve via php. Now I'm lost getting to the requested page while maintaining the pretty url. Can my approach work or what would be a better way?

推荐答案

您需要一个基础标签添加到您的部分,以便您可以使用相对URL你的CSS。

You need to add a base tag to your head section so that you can use the relative URL for your CSS.

<base href="http://example.com" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
//The CSS file above will load from http://example.com/css/style.css

您可以在link标签的路径之前,使其开始在根目录下添加一个斜杠。

You can add a forward slash before the path in the link tag so that it starts at the root directory.

<link href="/path/to/style.css" rel="stylesheet" type="text/css" />

这篇关于htaccess的重定向与数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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