homepage Welcome to WebmasterWorld Guest from 54.167.75.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mod rewrite
how do you do it
whatson




msg:4456382
 1:05 pm on May 22, 2012 (gmt 0)

Ok I will first explain exactly what I am trying to achieve.

I have pages like mydomain.com/category?id=5
I want them to be mydomain.com/categoryname

So instead of saying category?id=5, I want it to display the name of the category from the database.
Also when I create more categories in the admin, I then want these new pages to automatically update with the url like this.

Please let me know how this is accomplished.

 

rocknbil




msg:4456498
 4:37 pm on May 22, 2012 (gmt 0)

Is this custom coded? (presuming yes)

What you do is start with something like this. You will see code that says "if it's not a directory and it's not a file, then do this." Do not use that code, even though all CMS's use it. It searches the files system twice and is a real resource hog.

Since any included files you have will have a DOT (file.gif, flash.swf, style.css, javascript.js) you can evaluate all incoming URL's for a DOT. If it doesn't have a DOT, direct to index.php (presuming your script is index.php)

RewriteEngine on
RewriteRule ^([^.]+)$ /index.php [L,QSA]

Within that script, you evaluate your URL path via the $_SERVER['REQUEST_URI']. Depending on how you want to structure it, you'll identify either the terminal "part" that connects with the document or possibly other parts. In your example, if I request category-name/my-widgets

list($url,$querystring) = explode('?',$_SERVER['REQUEST_URI']);
// This gets rid of Google tags, etc.

$uri_parts = explode('/',$url);

$page_url = (isset($uri_parts[1]) and ! empty($uri_parts[1]))?$uri_parts[1]:$uri_parts[0];

$category = (isset($uri_parts[1]) and ! empty($uri_parts[1]))?$uri_parts[0]:null;

So now you look up the URL for that product/page.

$query = "select id from table where ";
if ($category) { $query .= " category='$category' and"; }
$query .= " page_url='$page_url'";

(Note the spaces in the concatenation, they are important)

This should give you
select id from table where category='category-name' and page_url='my-widgets';

From there you can figure out what happens next; use the ID like you would with $_GET['id']. Be sure to use error trapping and an "else" if no records are found - it will help you to debug. In any case, don't jump through hoops to use the page title as a URL - it's a pain in the a** and is subject to all sorts of problems, create a field just for the page URL.

This is but one way to do what you're asking, there are many, and I've left a lot of stuff out. This will get you started.

g1smd




msg:4456506
 4:54 pm on May 22, 2012 (gmt 0)

The very first thing you do is change the href links on your pages to use the URLs that you want users to see and use.

The format is up to you, but extensionless URLs will make for easier and more efficent code.

Once the link is clicked, your RewriteRule activates to map that incoming URL request to an internal file that will handle the request.

That script will need to look in the database for content. If there is no matching db record for this request the script must return the correct 404 HTTP headers and human readable error message. If there is content in the db, the script then assembles the HTML page and sends it to the browser.

You'll find life a lot easier if you use "/id-category" e.g. example.com/5-thispage for your URLs rather than just example.com/category . If you persist in using only the name, you have to make sure that the same page cannot be created twice and that reserved page names (such as robots.txt) cannot be created at all.

whatson




msg:4456777
 2:12 am on May 23, 2012 (gmt 0)

True, so when a new page name is created or edited, how can I check if the name already exists on another page?

g1smd




msg:4456785
 2:26 am on May 23, 2012 (gmt 0)

Look it up in the database.

However, if the URLs will include the unique page ID number, you don't have to do that step.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved