Jump to content

PHP and Pretty URLs


jostclan

Recommended Posts

I am beginning a version of our corporate website written with a in-house mySQL content database and PHP front end.  I am looking at using pretty urls (site.org\pretty-page-url) instead of ugly urls (site.org\page.php?id=34).  Without using Wordpress which I have no experience with is there a plugin or code I can use that can incorporate this feature?

 

I can add any number of columns to my table to pull the pretty url value if that helps.

Link to comment
Share on other sites

As long as you store "pretty-page-url" along with the page, and you guarantee that the value is unique across all pages, then it's easy.

 

Apache? Probably. Stick this in your .htaccess or virtualhost configuration:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)$ page.php?id=$1 [L]
That uses mod_rewrite to do URL rewriting by taking any request that doesn't match an existing directory or file, and doesn't have any slashes in it, then sends it to page.php.

 

Google "Apache URL rewriting" and spend a couple minutes reading through the results so you understand basically what it's all about.

 

Then code up page.php so that it uses $_GET["id"] to find the right content. Obviously you can change the file and query string to something else if you want. Make sure your links on the site use the correct "/pretty-page-url" and not "/page.php?id=pretty-page-url" because those won't be changed automatically for you.

Link to comment
Share on other sites

Archived

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

×
×
  • 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.