Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Website Design/Showcase (http://www.chiefdelphi.com/forums/forumdisplay.php?f=64)
-   -   PHP and URL's (http://www.chiefdelphi.com/forums/showthread.php?t=76180)

ZInventor 25-03-2009 02:13

PHP and URL's
 
We have a PHP website running on an Apache server, and want our current, query url's to look like normal urls (url instead of query string)

i think this can be done in apache, but don't know how.

basically, the website gets pages like this:

base URL =http://www.riverdalerobotics.com/

about us page: http://riverdalerobotics.com/?us

we want it to look like this:
http://riverdalerobotics.com/us

but, we don't want to have a ton of little tiny PHP pages everywhere on the server...

we only want it to LOOK like they are actual urls, but really be query's (have apache convert from query to url and back... so that the user sees the illusion of a real URL.

if you know of a way to do this your help would be much appreciated!

thanks,

-Z

artdutra04 25-03-2009 03:24

Re: PHP and URL's
 
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Look down at the RewriteRule, and you should find everything you need there.

ZInventor 25-03-2009 05:00

Re: PHP and URL's
 
i sort of get it, but since i haven't dealt much with Apache before, am still quite confused (i work in HTML, PHP and JavaScript)

would it be possible for you to post an example (the ones in the doc didn't make sense to me...)

thanks!

-Z

artdutra04 25-03-2009 06:21

Re: PHP and URL's
 
You need a .htaccess file in the base directory of where you want to start changing URLs. It can be even higher than this (such as out of the htdocs folder, hence out of web directory), but your URLs need to reflect this.

Using the Apache guide, the first thing you need to do is use the first line to turn the mod_rewrite engine on.

Then, you put one RewriteRule per line in the .htaccess file. Each line must start with RewriteRule, then a space, then a ^ (which signifies start of the URL you want users to see), then the URL (anything in brackets is a variable), then a $, then a space, then the actual file URL, with $1, $2, $3, etc being the respective variables from the first part. Example:

RewriteRule ^photogallery/([0-9]+)/([a-zA-Z0-9-]+)/([0-9])$ gallery.php?year=$1&gallery_id=$2&page_offset=$3

If gallery.php was located at example.com/gallery.php, then this RewriteRule would allow this URL

example.com/photogallery/2009/this-is-a-test/3
or
example.com/photogallery/2008/i-CAN-has-camelCASE-and-numb3r5/2

Edit: mod_rewrite is "cosmetic only", in that it does not permanently change the actual URL to the file. The real file can still be accessed by gallery.php?year=... url, even with mod_rewrite. The only thing mod_write does is provide a cosmetic placeholder URL to hide the real internal structure, parameters, and even the programming language used from users.

The first variable ([0-9]+) gets stuck where $1 placeholder is. The [0-9] part indicates this can accept any character from 0-9, and the + part indicates that it can have multiple digits. So 3, 34, 345, 3456, 34567 would all work here. Any character not allowed by this will throw an Error 404 on the URL.

The second variable, ([a-zA-Z0-9-]+) will accept all alphanumeric characters, with the addition of the dash - and gets put in the $2 placeholder. Any character not allowed by this will throw an Error 404 on the URL.

The third part ([0-9]) indicates this is only a single 0-9 digit, and nothing else.

Of course the rewrite_rules do not have to be this complex, but this was just an example. If you are looking to make URLs multiple artificial directories deep (such as foo/foo2/foo3), then make sure the HTML code you generate compensates. And last but not least, always put your most complicated and most restrictive cases first in the .htaccess file and work your way to simpler. This is because as soon as Apache finds a RewriteRule that works, it will use that and ignore the rest of the file.

If you get stumped, there are thousands of websites out there with much more detailed information about the intricacies of mod_rewrite that are only a Google search away.


All times are GMT -5. The time now is 16:07.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi