PHP and simple, static URLs

GJSNeptune

[H]F Junkie
Joined
Mar 31, 2004
Messages
12,372
I've always passed non-sensitive variables through URLs, but it can get very messy. I'm starting to revamp my solution so that URLs remain neat and simple while allowing certain content to still have static URLs.

Instead of links like:
  • http://www.domain.com/blog.php
  • http://www.domain.com/blog.php?article=1

Each subpage gets its own directory:
  • http://www.domain.com/blog
  • http://www.domain.com/forum
  • http://www.domain.com/fiction

Supposing I wants static URLs to instead look something like:
  • http://www.domain.com/fiction/Title_Of_Fiction_Piece

How should I go about doing this? Is it simply a matter of parsing the text entered into the URL and checking it against titles in the database?
 
How should I go about doing this? Is it simply a matter of parsing the text entered into the URL and checking it against titles in the database?

That's what you'd have to do, yeah. The normal way to do it is to use mod_rewrite to redirect to say /landing.php?title=Foo_Bar, and then have landing.php figure out what article to to be loaded and include() the correct code to render the requested page.

You could I suppose generate new rewrite rules for each new anchor you need instead. For articles this might be feasible since there probably won't be many of them, for blog posts I prefer looking up in the database.

Using a 'url alias' for the title of the post is a good idea, often shortened, all lowercase, no spaces or other punctuation etc. Just using the post id isn't so bad either for blog posts, it's not like people are going to memorize the URL of a blog post, they're going to use copy/paste or whatnot, in which case foo.com/blog/335 still looks pretty neat and is shorter than using a title.
 
Just using the post id isn't so bad either for blog posts, it's not like people are going to memorize the URL of a blog post, they're going to use copy/paste or whatnot, in which case foo.com/blog/335 still looks pretty neat and is shorter than using a title.
Not a bad method, but having the title in the URL is better for SEO.

I forgot about mod_rewrite. I'll have to look into that again. Thanks.
 
You want to look into Apache's mod_rewrite. You can write rules for that to translate the static URLs into php addresses with GET arguments.
 
Back
Top