Jump to content

Archived

This topic is now archived and is closed to further replies.

austar

Clean URLs with PHP

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--quoteo(post=346708:date=Feb 17 2006, 03:08 AM:name=Bane)--][div class=\'quotetop\']QUOTE(Bane @ Feb 17 2006, 03:08 AM) [snapback]346708[/snapback][/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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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:

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

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:

[code]
<Files *>
ForceType application/x-httpd-php
</Files>
[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.