Jump to content

Url Rewrite


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

I'm not sure if this is a PHP-related or Apache related question but I'll ask here anyways.On this page it tells you how to change URLs with IDs to search engine friendly URLs (fancy URLs).http://roshanbh.com.np/2008/02/hide-php-ur...g-htaccess.htmlNow my question is, how do you change the URL based on a date and title basis? Like the link above where it has "2008/02/hide-php-url-rewriting-htaccess".I think it needs to be done through PHP because it needs to find the article and get the title first. But headers could already be sent by then and that won't work, so what do I need to do? Also I would only like the URL to be rewritten if a certain GET variable equals a certain value, but that isn't important right now, getting the first part to work is most important.

Link to comment
Share on other sites

In that case it could be as simple as something like:

RewriteEngine OnRewriteRule ([0-9]+)/([0-9]+)/(.+) index.php?mode=articles&year=$1&month=$2&title=$3

That would give you the case where if you requested /<number>/<number>/<anything> (you can make the regex match more strict as needed) it would redirect to the articles page with $_GET['year'], $_GET['month'], $_GET['title'] all set to the appropriate segments of the URL. From there it handles like a typical query string request.

Link to comment
Share on other sites

Guest FirefoxRocks

But my problem is that right now I'm using $_GET["id"]. So my links look like this: http://example.com/page.php?id=2With your expression above, won't that mean the title field of my database need to be unique? And that is unacceptable because the articles could have the same title, even if I split it into categories.

Link to comment
Share on other sites

Well, either you [a] don't have equivalently titled articles within one month generate another unique slug that represents the article (e.g. the first title gets, e.g. "a-new-system" and the second one gets "a-new-system-2" or even "a-new-system/2") or [c] present a disambiguation page (bad).You'll probably want to generate URL-friendly slugs for each of your articles anyway, so is a good option.

Link to comment
Share on other sites

Alternatively you could choose to go for a different format and have 2009/08/039291/anything-here-it-doesnt-matter, where the article ID is embedded as the last number, but it still contains the title. This gives you the nice URL still, because it starts with the date and ends with the title, the messy bit is hidden in the middle there. In this case the title doesn't get taken as a $_GET variable (unless you want it to for something), and can be anything. The article ID is used instead like you want. Some newspapers' websites use this for example.So you generate links with the correct ID and the correct title at the end, but the title could in effect be anything, and duplication doesn't matter.

Link to comment
Share on other sites

Guest FirefoxRocks
Well, either you [a] don't have equivalently titled articles within one month generate another unique slug that represents the article (e.g. the first title gets, e.g. "a-new-system" and the second one gets "a-new-system-2" or even "a-new-system/2") or [c] present a disambiguation page (bad).You'll probably want to generate URL-friendly slugs for each of your articles anyway, so is a good option.
So how do I do ?
Link to comment
Share on other sites

So how do I do ?
You have another little function that converts spaces, special characters, whatever, to hyphens (Google recommends hyphens over underscores) for a given string (you can use regular expressions for this). Then you have an additional field in your table called 'slug' (WP term) or whatever. Then, when an article is created, you first pass the title through the converting function. Then, you check to see whether that title already exists in the database, and if so add -2 at the end (or as high as necessary so there isn't a collison). Then you store that slug in the DB, and use that for your selection queries.
Link to comment
Share on other sites

Personally, I like to have one simple URL rewriting rule that matches everything (except certain patterns for static files) and gives the URL "as is" to PHP. PHP could then map the URL to whatever it needs to be mapped to.I once wrote a topic about it somewhere, but I can't find it now. Anyhow, the idea is something like:

RewriteRule /[^_].+ /index.php?uri=$0

(in this form, it's probably not going to work)Where/something/in/a/folder/is mapped to/index.php?uri=/something/in/a/folder/while/_images/something.jpgremains as is.index.php could then take $_GET['uri'] and do various manipulations with it to find out the file it needs to open.

Link to comment
Share on other sites

Well, assuming AElliot's code works, you could use that. Then, in PHP, just SELECT using the slug instead of the actual title. Or you could use boen_robot's method.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...