|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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. Last edited by artdutra04 : 25-03-2009 at 06:25. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP and MySQL | MrToast | Website Design/Showcase | 4 | 18-03-2005 22:29 |
| PHP and Wiki Error | Venkatesh | Website Design/Showcase | 2 | 24-07-2004 15:51 |
| PHP, MySQL, and Dropdown lists | Raven_Writer | Website Design/Showcase | 22 | 01-07-2004 08:05 |
| php and gzip compression | Trashed20 | Website Design/Showcase | 2 | 12-06-2003 22:11 |