Here we will be utilizing the scary .htaccess that most webservers have by default, don't have one? Create a file with the name .htaccess Yes, with the dot with your FTP client in your root web folder and you'll be on the way to utilizing nice urls!
Update: I had written a similar tutorial after this, but for "nice" URL titles of which you can use in your application. An example is turning ?id=41 into ?id=about-us. The tutorial is here:
http://forum.codecal...tles-slugs.html
What does .htaccess do to urls exactly?
Plenty of websites,Including CodeCall here use an Apache feature called mod_rewrite to make urls nicer and cleaner for users and search engines alike. A simple example of the page you may be viewing now is the following:
http://forum.codecall.net/php-tutorials/31271-nice-urls-htaccess-php.htmlWell that's nice, but what are we really seeing?.
Here is the actual thread, go ahead and paste it into your browser to see it works:
http://forum.codecall.net/forum/showthread.php?t=268346The original is boring, glad mod_rewrite was there to fix it!
----------------------------------------------------------------------------------------------
Well! Lets get down to examples!:
You may want to spoof your site up in a simple way, yes? Here's your example boring layout:
Layout:
[noparse]http://www.site.com/...x.php?page=home
http://www.site.com/....php?page=about
http://www.site.com/...?page=downloads[/noparse]
Lets transform it a little with a .htaccess file! Here we add it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]With that done, and without any actual modifying if your .php files, this will work:Layout:
[noparse]http://www.site.com/home
http://www.site.com/about
http://www.site.com/downloads[/noparse]
Neat, eh?: The use of the .htaccess file we defined will automatically plug "about" into index.php?page=about.
And that concludes the simple lesson! Enjoy, and be sure to remember if you mess up the .htaccess file it may hand you a scary 500 error on your site, just remove it to fix it and be sure you typed things right.
I will edit back and include more examples when I get the time,
Thank you for reading!
Edited by Alexander, 17 October 2010 - 08:41 PM.
Updated to add link to my other tutorial


Sign In
Create Account

Back to top










