Clean URLs with PHP
#1
Posted 16 February 2006 - 11:48 PM
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
Posted 17 February 2006 - 12:29 AM
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
Posted 17 February 2006 - 09:49 AM
rc69, on Feb 17 2006, 05:29 AM, said:
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
Posted 17 February 2006 - 09:57 AM
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.phpthen 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
Posted 17 February 2006 - 12:55 PM
#6
Posted 17 February 2006 - 02:04 PM
http://www.yoursite.com/style.css
#7
Posted 17 February 2006 - 05:04 PM
austen, on Feb 17 2006, 07:49 AM, said:
rc69, on Feb 16 2006, 10:29 PM, said:
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
Posted 17 February 2006 - 07:42 PM
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
Posted 17 February 2006 - 08:07 PM
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
Posted 17 February 2006 - 08:20 PM
rc69, on Feb 18 2006, 01:07 AM, said:
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
Posted 18 February 2006 - 01:55 AM
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
Posted 18 February 2006 - 01:33 PM
austen, on Feb 17 2006, 06:20 PM, said:
-austen
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
Posted 18 February 2006 - 06:34 PM
rc69, on Feb 18 2006, 06:33 PM, said:
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/
thanks for all the help everyone!
-austen
#14
Posted 02 March 2006 - 02:37 PM
if you need help just PM me
#15
Posted 02 March 2006 - 02:48 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
