Jump to content

Php Dynamic Urls, Seo Friendly?


NewcastleFan

Recommended Posts

Hey everyone,

 

I'm working on teaching myself php/mysql by build a Content Management System I've got to a certain point which I'm quite proud of but I've hit a bit of a bump.

 

at current my dynamic page urls are "index.php?pid=1" However I would like them to be actual words e.g. "/home-page" "/example-page".

 

I have no idea how to even begin to do this, I've seen a lot of posts about using .htacess and url re-writes but I don't seem to be able to get these to work.

 

Current build menu code in functions.php:

 


// ***************************************
// Start Build Menu
// ***************************************
$sqlCommand = "SELECT id, linklabel, seourl FROM pages WHERE showing='1' ORDER BY pageorder ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$seourl = $row["seourl"];
$menuDisplay .= '<a href="index.php?id=' . $pid . '">' . $linklabel . '<a><br .>';
}
mysqli_free_result($query);

// ***************************************
// End Build Menu
// ***************************************

 

I have added the extra seourl field into the database so when a new page is created it grabs the page title, removes the spaces and replaces them with - so a page called example page one will turn into example-page-one.

 

However I am completely lost on how to use the seourl to load the content in the page.

 

This is also how I currently pull the content from the database for the page info.


// ***************************************
// Select Page ID and Content, Keywords/Description
// ***************************************

if (!$_GET['id']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['id']);
}

$sqlCommand ="SELECT pagebody, description, keywords FROM pages WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body = $row["pagebody"];
$desc = $row["description"];
$keywords = $row["keywords"];
}
mysqli_free_result($query);

// ***************************************
// Select Page ID and Page Title
// ************************************

 

If anyone has any advice or can point me in the right direction that would be fantastic!

 

Thanks

Link to comment
Share on other sites

Not quite the only way, but you will have to use the file name in the URL.

If you write an URL like this:

<a href="my.domain.com/index.php/test/id=3">Page 3</a>

Then you'll find all of the stuff after the filename in the $_SERVER['PATH_INFO'] index, which you can then run through a RegExp to expand into an array (or add it to the $_GET superglobal). That's what I've done in my framework, and it works a treat no matter which server I've tested it on.

 

Best part: No .htaccess rewrite rules needed. ;)

Link to comment
Share on other sites

Not quite the only way, but you will have to use the file name in the URL.

If you write an URL like this:

<a href="my.domain.com/index.php/test/id=3">Page 3</a>

Then you'll find all of the stuff after the filename in the $_SERVER['PATH_INFO'] index, which you can then run through a RegExp to expand into an array (or add it to the $_GET superglobal). That's what I've done in my framework, and it works a treat no matter which server I've tested it on.

 

Best part: No .htaccess rewrite rules needed. ;)

 

This sound way more complicated than I thought :(

 

I've tried again with .htaccess but using this I've managed to get nothing to change:

 

 

Options +FollowSymLinks

RewriteEngine on

 

RewriteRule /id/(.*)/ index.php?id=$1

 

 

I cant even get my page ID's to be /id/1 never mind a nice looking text link :(

 

Anyone able to help me out?

 

Thanks

Link to comment
Share on other sites

Remove the last slash from the rewrite rule, and it should work.

 

However, my solution isn't all that complicated. All you need is one IF test and one loop, and the IF test looks like this:

// Split path, and check if any parameters were found.
if (!preg_match_all ("#/(?[^=/]+)=([^/&]+))|([^/&]+)#", $_SERVER['PATH_INFO'], $Matches, PREG_SET_ORDER)) {
	// None found, abort.
	return;
}

 

I've added mine inside a function, thus the use of return to stop the parsing if no matches are found.

Link to comment
Share on other sites

Remove the last slash from the rewrite rule, and it should work.

 

However, my solution isn't all that complicated. All you need is one IF test and one loop, and the IF test looks like this:

// Split path, and check if any parameters were found.
if (!preg_match_all ("#/(?[^=/]+)=([^/&]+))|([^/&]+)#", $_SERVER['PATH_INFO'], $Matches, PREG_SET_ORDER)) {
	// None found, abort.
	return;
}

 

I've added mine inside a function, thus the use of return to stop the parsing if no matches are found.

 

Ha, yeh that's way above my knowledge at the moment I think :( .htaccess is getting on my nerves are either nothing happens or i get a 500 server error.

 

How would I go about implementing what you suggested with php into my original post code?

Link to comment
Share on other sites

Hi, this is the .htaccess file that i wrote for the last website i built:

 

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php
RewriteRule ^product/([a-zA-Z0-9_-\s\'\,\(\)\.]+)$ product.php?product=$1
RewriteRule ^category/([a-zA-Z0-9_-\s\'\,\(\)\.]+)$ category.php?category=$1
RewriteRule ^category/([a-zA-Z0-9_-\s\'\,\(\)\.]+)/price/([0-9]+.[0-9]+)-([0-9]+.[0-9]+)$ category.php?category=$1&lower=$2&upper=$3
RewriteRule ^category/([a-zA-Z0-9_-\s\'\,\(\)\.]+)/([0-9]+)$ category.php?category=$1&pageNo=$2
RewriteRule ^page/([a-zA-Z0-9_-\s\'\,\(\)\.]+)$ page.php?page=$1
RewriteRule ^search/([a-zA-Z0-9_-\s\'\,\(\)\.]+)$ search.php?search=$1
RewriteRule ^search/([a-zA-Z0-9_-\s\'\,\(\)\.]+)/([0-9]+)$ search.php?search=$1&pageNo=$2
RewriteRule ^reset-password/([0-9]+)/([a-zA-Z0-9_-]+)$ log.php?id=$1&authCode=$2
RewriteRule ^orders/([0-9]+)$ orders.php?pageNo=$1
RewriteRule ^view-order/([a-zA-Z0-9_-]+)$ view-order.php?id=$1
RewriteRule ^view-order/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-order.php?id=$1&authCode=$2
RewriteRule ^news/([0-9]+)$ News.php?pageNo=$1
RewriteRule ^news/article/([a-zA-Z0-9_-\s\'\,\(\)\.~\%\*]+)$ article.php?pageNo=$1

ErrorDocument 404 /404.php

 

it all works perfectly.

Edited by White_Lily
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.