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

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.