Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Nice urls with .htaccess and PHP!


  • Please log in to reply
19 replies to this topic

#1 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 11 August 2010 - 02:49 PM

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

 

Update2: Orjan has written a superseding tutorial to this:

http://forum.codecal...-urls-with-php/

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:
http://www.site.com/home
http://www.site.com/about
http://www.site.com/downloads


Neat, eh?: The use of the .htaccess file we defined will automatically plug "about" into index.php?page=about.

 

Note that the !-f and !-d tags are to prevent the rewrite of files that share the same name as the url, such as /foo being a file, whereas foo-about may not be a file and therefor be a web page.

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, 01 October 2014 - 08:13 PM.
Listed superseding tutorial

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#2 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 11 August 2010 - 04:25 PM

Nice, tutorial, Nullw0rm. I love using .htaccess with all of my sites.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#3 profzor101

profzor101

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 14 August 2010 - 08:25 PM

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

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 16 August 2010 - 01:50 AM

Thanks!
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 profzor101

profzor101

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 16 August 2010 - 01:54 AM

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

#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 16 August 2010 - 04:04 AM

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. :)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#7 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 27 August 2010 - 11:17 AM

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??
  • 0
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

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 27 August 2010 - 09:35 PM

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

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#9 lancewhear

lancewhear

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 November 2010 - 08:17 PM

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??
  • 0

#10 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 06 November 2010 - 08:36 PM

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?
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#11 lancewhear

lancewhear

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 November 2010 - 09:33 PM

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

#12 krmprasanna

krmprasanna

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C
  • Learning:C

Posted 27 September 2012 - 10:47 AM

hai
i need sample code for clean url, plsz send me
  • 0