Jump to content


Photo

Clean URLs with PHP


  • Please log in to reply
6 replies to this topic

#1 austar

austar
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationMT

Posted 17 February 2006 - 05:04 AM

Hey guys... though I've done a heck of a lot of projects in PHP, I've never meddled with making things into clean URLs. When I say "Clean URL" I mean URL's such as on the Tutorial part of the site, (i.e. [a href=\"http://www.pixel2life.com/tutorials/Adobe_Photoshop/All/)\" target=\"_blank\"]http://www.pixel2life.com/tutorials/Adobe_Photoshop/All/)[/a]. Does anyone know how to do this with some PHP and .htaccess?? I would prefer not to use mod_rewrite, though I don't really care. I need to know

1. How to make www.domain.com/contact.php into www.domain.com/contact/

2. How to make www.domain.com/page.php?id=123&cat=abc domains into www.domain.com/page/123/abc

Any help, or nods in the right direction would be GREATLY appreciated, thanks guys!

-austen
Neo-Maxi-Zoom-Dweebie.

#2 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 17 February 2006 - 06:35 AM

Yeah this is actually a really good question. I'm pretty new to PHP myself and I'm very interested in learning how to do this as well.

#3 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 17 February 2006 - 10:08 AM

you can achieve this affect with PHP...but it's best you do it via your server which is standard practice.

in Apache, this is called "Mod Rewrite"

the first 5-6 links here will be useful to you:
[a href=\"http://www.google.com/search?q=mod%20rewrite&hl=en\" target=\"_blank\"]Google: mod rewrite[/a]


you might need help with writing the rewrite rule itself which might be hard to find via google and if that's the case, please do post here and one of us will help you.


i'll move this to the Apache forum since this is Apache specific

#4 austar

austar
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationMT

Posted 17 February 2006 - 02:53 PM

[!--quoteo(post=346708:date=Feb 17 2006, 03:08 AM:name=Bane)--][div class=\'quotetop\']QUOTE(Bane @ Feb 17 2006, 03:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you can achieve this affect with PHP...but it's best you do it via your server which is standard practice.

in Apache, this is called "Mod Rewrite"

the first 5-6 links here will be useful to you:
[a href=\"http://www.google.com/search?q=mod%20rewrite&hl=en\" target=\"_blank\"]Google: mod rewrite[/a]
you might need help with writing the rewrite rule itself which might be hard to find via google and if that's the case, please do post here and one of us will help you.
i'll move this to the Apache forum since this is Apache specific
[/quote]

Thanks Bane, I'm aware that you can do it with mod_rewrite, though I was wondering if there were any alternate solutions, just so I know.

Thanks for all your help guys, it's really appreciated!!

-austen
Neo-Maxi-Zoom-Dweebie.

#5 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 17 February 2006 - 04:44 PM

ah ok, since you had mentioned .htaccess, i assumed you meant mod rewrite.

anyway, here is a PHP alternative.
it's been done before many times, this is just my version of it.

[a href=\"http://koobi-studio.com/downloads/php/functions/seoUrl/index.php?source=1\" target=\"_blank\"]Koobi-Studio: Search Engine Friendly URL's[/a]

let me know if there's anything else :)

#6 austar

austar
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationMT

Posted 18 February 2006 - 12:43 AM

Ok guys, I'm pretty sure I'm REALLY close, though I need a little help.... I've looked at a LOT of different resources on mod_rewrite and am ALMOST there. I'm trying to make it so that apache will see a file (any file with any name and a .php extension) in the url and then redirect that url from www.domain.com/index.php to www.domain.com/index THAT part I am succesful at doing, now it is making the server think that /index points to that /index.php file.

Here is my .htaccess right now:

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([a-zA-Z0-9]+)\.php$ /$1 [R]

This code DOES work, and won't throw the server into a infinite loop because it HAS to have the .php extension to be picked up by the server and it outputs things without that extension.
NOW, the problem is, this throws 404 errors because it's looking for a file named "index" with no extension, therefore, this WON'T work unless I make every one of my .php files no longer have an extension, which would suck and I won't do. This I know won't work though:

<Files *> 
ForceType application/x-httpd-php
</Files>

The reason why that wouldn't work is the fact that I'm not going to have a ton of files floating around with no extension, it's messy, and pointless.

AHHH I'm pulling out my hair, does anyone know what I could do?

-austen
Neo-Maxi-Zoom-Dweebie.

#7 austar

austar
  • Members
  • Pip
  • Newbie
  • 7 posts
  • LocationMT

Posted 18 February 2006 - 06:55 AM

Excuse the double post, but I have FINALLY found a solution for removing the extension in the URL.
After much extensive searching, google finally led led me to

[a href=\"http://mjtsai.com/blog/2004/12/27/anchors-and-cruft-free-urls/\" target=\"_blank\"]This Blog[/a]

This can be substituted for any file format, I used ".php" since that was what my files were. I will not go into details as to how this code works, though if you want to get it to work, simply replace the "shtml" in this guy's code with the extension that your files are using.

Now instead of the URLs being [a href=\"http://domain.com/pagename.php\" target=\"_blank\"]http://domain.com/pagename.php[/a] they are simply [a href=\"http://domain.com/pagename\" target=\"_blank\"]http://domain.com/pagename[/a] which makes everything much more simple and clean.

-austen
Neo-Maxi-Zoom-Dweebie.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users