Reputation: 197
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.
I'm still trying to use htaccess to change this not so lovely url
http://localhost/test/index.php?page=Article&articleID=61
to
http://localhost/test/article/2015-09-21-this-is-the-headline
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]
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.
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
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: 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?
Upvotes: 2
Views: 210
Reputation: 24448
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
OR
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" />
Upvotes: 2