Friendly URL Not Working?

Hi, hoping someone can help, think it’s something small. I’m trying to make a friendly URL. I’ve added the Rewrite Rule to my .htaccess as below:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\\.]+)$ $1.php [NC,L]

RewriteEngine On

RewriteRule pass/(.*)$ pass.php?section=$1 

However, the page is appearing with the text but the data from the database is not displayed at all, the only text displayed is ‘Your name is’ and ‘Your date of birth is’.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>

<?php
include_once('connectionpass.php');
$section = $_GET['section']; // get var from URL
$result = mysql_query("SELECT * FROM birthdays WHERE section = $section");
$row = mysql_fetch_array($result);
?>
Your name is, 
<?php echo $row['firstname'];?> 
<?php echo $row['lastname'];?>.
Your date of birth is <?php echo $row['birthday'];?>.
</body>
</html>

My SQL is below:

-- phpMyAdmin SQL Dump
-- version 3.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 26, 2010 at 12:27 AM
-- Server version: 5.1.44
-- PHP Version: 5.2.13

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `newdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `birthdays`
--

CREATE TABLE `birthdays` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `section` varchar(30) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `birthday` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `birthdays`
--

INSERT INTO `birthdays` VALUES(2, 'about-us', 'Kriss', 'Kerr', '19/11/1990');
INSERT INTO `birthdays` VALUES(3, 'hello', 'Marc', 'Batty', '19/11/1990');

I’m sure it’s something so simple that I have missed or something.

Try this:


$section = isset($_GET['section']) ? $_GET['section'] : false ; // get var from URL
if($section) {
$result = mysql_query("SELECT * FROM birthdays WHERE section = '".mysql_real_escape_string($section)."'");
// rest of script
} else {
    die('no section specified');
}

You weren’t escaping your database input ($section), which makes the site easily hackable and caused your query to fail.

Look into using something like [fphp]pdo[/fphp] to better protect your database.

Anyone got any ideas as to what could be causing this to not work? Really appreciate the help.

Maybe you have no records in your database? You’re not even checking whether there was an error within the query. All you do is send the query and immediately you try to parse the data, which doesn’t exist for some reasn.
Use mysql_error() function to examine whether there were errors and check whether you have entries in the database.

Hi, Immerse, I have tried your fix but still doesn’t seem to want to display the results. I can’t work out what in the earth is causing this not to show? Now it’s just saying ‘no section specified’.

Blue, if you view by original post, you’ll see that I posted the SLQ and you’ll see the records and their values. I’ve had a shot at the mysql_error but doesn’t seem to do anything.

My updated PHP:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>

<?php
$section = isset($_GET['section']) ? $_GET['section'] : false ; // get var from URL
if($section) {
$result = mysql_query("SELECT * FROM birthdays WHERE section = '".mysql_real_escape_string($section)."'");
// rest of script
} else {
    die('no section specified');
} 
?>
Your name is, 
<?php echo $result['firstname'];?> 
<?php echo $result['lastname'];?>.
Your date of birth is <?php echo $result['birthday'];?>.
</body>
</html>

What’s the value of $section? Look in the address bar and / or call var_dump( $section ) after setting the variable.

I’m not sure what you’re meaning? I think it’s probably best I start afresh…anyone know of any good tutorials to show how this is done? Found a few but they’re based just on the .htaccess. I need to learn the PHP too.

Actually, since you’re creating the query string in a rewrite, you couldn’t just look at the address bar, so var_dump( $section ) would be the way to go, but if you don’t know what that or “setting the variable” means, then learning more PHP would make sense at this point.

What do you want to achieve; do you want to redo this for this page only, or for a complete application?

As mod_rewrite rules start with ^, your second rule should be ^pass/(.*)$. Try to output the GET parameter to see if its functional, have you made sure that mod_rewrite returns the correct parameter?

Sorry if I write somewhat badly, I am tired.

P.S. Look into using mysqli, and the object oriented methodology with mysqli objects. It is very useful.

Also it is sufficient to set the RewriteEngine to on once in one htaccess.

Hi, thanks for your message. Ok, what I want to do is have a one-page website effectively. Depending on whatever the name is in the url e.g about-us then I want the data to display.

However, no data is displaying. The page is showing but with no data from the database. I’m wondering if I’m not echoing properly? Any ideas?

<?php echo $result['firstname']?>

Good news! Got the data to display now. So now I just need to get the URL to change from http://localhost:8888/index.php?section=about-us to just http://localhost:8888/about-us

Hoping to get this thread put to sleep now.

Try to set your htaccess as follows

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^pass/(.*)$ pass.php?section=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]

edit: Ok. Wasn’t it pass.php?

You my friend are an absolute hero! It’s working now beautifully. Only thing is I would prefer not to have pass in the URL but just …com/about-us

Tried taking out pass from the rewrite rule but doesn’t have it. If this isn’t easily doable I am more than happy with it how you have it. Really appreciate your help, so pleased you got it working.

I read your second post with the updates only after you posted the replies to my first post. The GET[‘section’] was not specified because of mod_rewrite not rewriting the URL as you intended it to (It seems you have figured that out now though), if you spend some more time practicing programming you will be able to interpret these kind of matters yourself. I take it you are a beginner, more or less. It is good foresight to apply friendly URL’s and database content already, you will surely learn how to apply this more generally.

Beaumont suggested using the var_dump function, look it up, it is very useful when debugging.

It is redundant to make comments like ‘// getting var from url’, when this is already clear from the program. A friendly advice on good documentation is to comment on more general matters, such as why something is written rather than what is written, and the bigger picture of the application. Also in more serious context such commenting will have it appear that you do not know how to program. It can be useful as study notes or as reminders before writing the application, but keep that in mind about documentation in general.

Returning to what you are working on achieving; the best is to define a mod_rewrite which assigns the different URL elements (separated by ‘/’) to GET parameters, and have one main index file for the application which handles the rest. There are two main alternatives for how to go about this; you can either have mod_rewrite do the separation into variables for you (see below), or you could do this with PHP from one mod_rewrite variable.

You can also have a mod_rewrite for different .php pages that you use, though off hand I see no reason to do this, and you seem already to have intended the previous.

Here is an example

RewriteEngine on

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?parameter1=$1&parameter2=$2
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?parameter1=$1

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?parameter1=$1&parameter2=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?parameter1=$1

This will match alphanumeric characters as well as hyphens and underscores.

In this specific scenario, assuming you have a pass.php file (still a bit ambiguous), defining the first rule as

^(.*)$ pass.php?section=$1

should solve that. If not try

^([a-zA-Z0-9_-]+)$ pass.php?section=$1

Hi there, yeah that’s perfect all working swimmingly now. Really appreciate your help. It’s only a basic site for a hairdessing salon and thought it would be an easier way to maintain the site, just working on the backend of the site now and coming together well.

Once again, appreciate the help. Was a member on another forum, quite a well known one but there is pretty much no help there. Here, there is so much help and people who genuinely want to help each other. It’s fantastic.