Jump to content


Clean URLs with PHP


14 replies to this topic

#1 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 16 February 2006 - 11:48 PM

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. http://www.pixel2lif...Photoshop/All/). 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

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 February 2006 - 12:29 AM

Since domain.com/contact/ would look for a contact folder before any php is processed, you're stuck with .htaccess. And the only thing in .htaccess i know of that can change a url is mod_rewrite.

If you don't like mod_rewrite because of the regex, there's a great article in "the answers to all your questions" sticky in this forum. But if you simply can't use mod_rewrite due to server issues, then i believe you're s-o-l.

#3 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 17 February 2006 - 09:49 AM

View Postrc69, on Feb 17 2006, 05:29 AM, said:

Since domain.com/contact/ would look for a contact folder before any php is processed, you're stuck with .htaccess. And the only thing in .htaccess i know of that can change a url is mod_rewrite.

If you don't like mod_rewrite because of the regex, there's a great article in "the answers to all your questions" sticky in this forum. But if you simply can't use mod_rewrite due to server issues, then i believe you're s-o-l.

Nope, mod_rewrite is fine actually, I was just wondering if there were any other ways to do it.... and does anyone know HOW to do it with a .htaccess file?

#4 Stu

    Retired P2L Staff

  • Publishing Betazoids
  • PipPipPipPip
  • 1,761 posts
  • Gender:Male

Posted 17 February 2006 - 09:57 AM

yes you can actually do it with PHP, but imo it seems overly complicated so i just stick with mod_rewrite.

tutorial on how to do it with php: http://www.tutorio.com/tutorial/php-altern...e-friendly-urls

using htaccess with your example would go something like...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^contact/$ contact.php
then change all your links over from contact.php to contact/ and it should work, but, you might have to change your images, styles, links - to full urls' or maybe add a <base> thingi because i've done this before and its been known to just leave out all my style sheets and images...

although im not too sure why this is neccessary in the above case, seeing has the main point of clean urls is to aid SEO, and with a link like contact.php it should be fine.

anyways here is a good tutorial on mod rewrite which should help : http://arutha.co.uk/viewtut/77/

Edited by Stu, 17 February 2006 - 11:48 AM.


#5 pihl

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 17 February 2006 - 12:55 PM

a problem this will generate is you have to change your CSS-linking to import an url or use your elite php-coding skills to link it from the root of your server :)

#6 Stu

    Retired P2L Staff

  • Publishing Betazoids
  • PipPipPipPip
  • 1,761 posts
  • Gender:Male

Posted 17 February 2006 - 02:04 PM

or just add a full url like

http://www.yoursite.com/style.css

:)

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 February 2006 - 05:04 PM

View Postausten, on Feb 17 2006, 07:49 AM, said:

...does anyone know HOW to do it with a .htaccess file?

View Postrc69, on Feb 16 2006, 10:29 PM, said:

...there's a great article in "the answers to all your questions" sticky in this forum.
http://www.pixel2lif...?showtopic=8980 To use .htaccess, just create a file (the full name of which, extension and all, is ".htaccess"), and upload it to your public_html directory (or which ever directory it is that gets loaded when people go to http://yourdomain.com/)

note: i would recommend the <base> tag for the above mentioned style problems, and then make all links from the root directiory (i.e. <link href="/style.css"> as opposed to <link href="style.css">)

Edited by rc69, 17 February 2006 - 05:05 PM.


#8 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 17 February 2006 - 07:42 PM

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

#9 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 February 2006 - 08:07 PM

I believe you're misunderstanding how mod_rewrite is supposed to work, because you're going backwards.

Generally, you would want to take the url "http://domain.com/index/" and have apahce direct you to "http://domain.com/index.php", you are currently doing the exact opposite of that.

I might be missing something, but as it stands right now, if you reversed the way you were doing things, it could help.

#10 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 17 February 2006 - 08:20 PM

View Postrc69, on Feb 18 2006, 01:07 AM, said:

I believe you're misunderstanding how mod_rewrite is supposed to work, because you're going backwards.

Generally, you would want to take the url "http://domain.com/index/" and have apahce direct you to "http://domain.com/index.php", you are currently doing the exact opposite of that.

I might be missing something, but as it stands right now, if you reversed the way you were doing things, it could help.

No, the whole point of me doing this is to get the url http://domain.com/page.php to be http://domain.com/page/ without having to put that page into its own folder.


-austen

#11 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 18 February 2006 - 01: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

This Blog

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 http://domain.com/pagename.php they are simply http://domain.com/pagename which makes everything much more simple and clean.

-austen

#12 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 February 2006 - 01:33 PM

View Postausten, on Feb 17 2006, 06:20 PM, said:

No, the whole point of me doing this is to get the url http://domain.com/page.php to be http://domain.com/page/ without having to put that page into its own folder.


-austen
Exactly why i think you're confused. Example .htaccess:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)[/]?$ /$1.php [R]
That would get the desired effect.

Note: I posted this without reading that link you provided.

#13 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 18 February 2006 - 06:34 PM

View Postrc69, on Feb 18 2006, 06:33 PM, said:

Exactly why i think you're confused. Example .htaccess:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)[/]?$ /$1.php [R]
That would get the desired effect.

Note: I posted this without reading that link you provided.

Actually, that doesn't work out because it ends up throwing 404 errors out. I know this because that's what my .htaccess looked like before. The method shown in the link I provided is much more extensive and functional, keeping in mind that what I wanted to do was to be able to have links on my pages that point to "pagename.php" and instead of /pagename.php in the url I wanted it to be /pagename and it's finally working, the script in the link I posted even checks to see if the url has index in it and if it does, it just removes it.

For example:
a link to domain.com/index.php would just be rewritten to domain.com/ :blink:

thanks for all the help everyone!

-austen

#14 FFX

    Young Padawan

  • Members
  • Pip
  • 105 posts

Posted 02 March 2006 - 02:37 PM

i use htaccess and im in middle of finishing it. but once you get the hang of it .htaccess is very easy.

if you need help just PM me

#15 Jamie Huskisson

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 3,648 posts
  • Gender:Male
  • Location:Nottingham, UK

Posted 02 March 2006 - 02:48 PM

I'll write an article on this eventually... too many people that don't know how to use it :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users