Jump to content

Nice urls with .htaccess and PHP!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Intro:
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.html
Well 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=268346
The 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

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#2
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
Nice, tutorial, Nullw0rm. I love using .htaccess with all of my sites.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#3
profzor101

profzor101

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks ALOT Nullworm, I just tested it out and it worked perfectly for my site, this is such a neat tool.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Thanks!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
profzor101

profzor101

    Newbie

  • Members
  • Pip
  • 6 posts
Awesome, My main site doesn't use an index with GET though, the layout is somewhat like this:
/login.php
/register.php
/startup.php
/SOAP.php

how would I rewrite it so I can go to myurl.com/about and it go directly to myurl.com/about.php? I'm actually quite excited about it,
greets, Profzor101.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
This is a fairly simple one, all we need to rewrite it into the filename, notice how I use the regular expression to match and the position of $1 can be changed to suit your needs
#login => login.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Glad I could help. :)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
Does this mean I can have all my tutorials with neat adresses (e.g. localhost/tutorials/Begining_HTML) without mucking around with my CSS adresses??
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

bbqroast said:

Does this mean I can have all my tutorials with neat adresses (e.g. localhost/tutorials/Begining_HTML) without mucking around with my CSS adresses??
From what you've said, you don't need to change anything. It only affects a file that doesn't exists, "Begining_HTML" is not a real file, so .htaccess takes care of it, but any image or css document does exist (a direct URL) so it will appear normally.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
lancewhear

lancewhear

    Newbie

  • Members
  • Pip
  • 2 posts
top code man... tried it out, each link kept referring back to the home page...



http://www.concit.co..._1_Welcome.html

http://www.concit.co...ig Reviews.html


any ideas??

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

lancewhear said:

top code man... tried it out, each link kept referring back to the home page..

My code would convert this:
concit.com/organisations/sydneysongwriters/1_1_Welcome.html
into this:
concit.com/organisations/sydneysongwriters/index.php?p=1_1_Welcome.html

Both your addresses are not using .htaccess rewrites though it seems, they both link to the homepage anyway, how are you using it?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
lancewhear

lancewhear

    Newbie

  • Members
  • Pip
  • 2 posts
at first i put the rewrite in the root directory to see if it did anything, then i put it in the .htaccess folder where the index.php n everything else trees down from.