Page 1 of 4

Clean URLs for a PHP rookie

Posted: Sat Mar 03, 2007 8:28 pm
by Bill_VA
I'm a ColdFusion guy from way back (since version 1.5) and am new to PHP and eager to learn. I'm developing an informational site that will/could host many cities and provide information about them. I want to use one domain name http://www.domain.com and have each city or locale use http://www.domain.com/thiscity to access the Thiscity version of the site, by querying the database and pulling up that city's info without having a physical folder on the server for Thiscity. Further, I'd want to use http://www.domain.com/thiscity/function ... nctionName to act as url variables.

So: http://www.domain.com/thiscity/thispage/thisfunction

would actually be calling the index.php file every time and passing info to predetermined variables

city = thiscity (like "Alexandria")
page = thispage (like "parks")
function = thisfunction (like "displayParks")

The following are assumptions you should know prior to answering my question:
- I'm fairly new to PHP and Linux, but not new to developing websites or databases (12+ years)
- The site will be hosted on someone else's server and I will/may not have access to files outside the one assigned to this domain.

Be gentle and explain yourselves fully. Thanks so much.

Posted: Sat Mar 03, 2007 8:47 pm
by tecktalkcm0391
First you'd have to use mod_rewrite to get the URL you wanted, and then to pass the variables it would be something like this:

The website address would really be... site.com/index.php?city=thiscity&page=thispage&function=thisfunction
To get the variables in PHP you would do this:

Code: Select all

<?php
$city = $_GET['city'];
$page= $_GET['page'];
$function= $_GET['function'];
// but remember that this isn't always secure you should make some code to check to make sure what is in the URL is vaild, because anyone can change it and mess up your site
?>
As for mod_rewrite.... edit the .htacces file in your public_html or equivlent folder and add something like this:

Code: Select all

#Below turns on mod_rewrite to change the url type
RewriteEngine On
#now the change 
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+) index.php?city=$1&page=$2&function=$3 [NC]

Posted: Mon Mar 05, 2007 9:56 am
by Bill_VA
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Sorry Chris, but that isn't working for me, I'm getting 404 errors.  Here's what I've got:

httpd.conf file has been edited to say:

Code: Select all

AllowOverride All
I now have a .htaccess file in the root of my folder /srv/www/htdocs/mysite/.htaccess which reads:

Code: Select all

RewriteEngine On 

RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?city=$1&page=$2&function=$3 [NC]
my index.php file has:

Code: Select all

<?php 
$city = $_GET['city']; 
$page= $_GET['page']; 
$function= $_GET['function']; 

print "here are the vars: $city, $page, $function";
?>
What am I doing wrong?
When I type this:

http://127.0.0.1/mysite/thiscity/thispage/thisfunction

in the browser I get a 404 file not found error. I want this to interpret that url into this:

http://127.0.0.1/mysite/index.php?city= ... isfunction


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Mar 05, 2007 3:24 pm
by RobertGonzalez
Are the pages that you are testing locally in your server root, or are they in a subdirectory of root?

Posted: Mon Mar 05, 2007 3:28 pm
by Bill_VA
In a subfolder (/srv/www/htdocs/mysite/index.php)

Thanks for picking up on this thread.

Posted: Mon Mar 05, 2007 3:35 pm
by RobertGonzalez
In your local .htaccess file, before the index.php instruction, prepend it with /mysite, so it looks like

Code: Select all

RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /mysite/index.php?city=$1&page=$2&function=$3 [NC]
Start with that. Also, have a look at the cheatsheets in the useful posts sticky in the Installation and Configuration forum. There are a lot of useful mod_rewrite tutorials available for you.

Posted: Mon Mar 05, 2007 4:05 pm
by Bill_VA
Sorry man, still a no-go. I used the sample code from the cheat sheet and still nothing. More help please.

Posted: Mon Mar 05, 2007 4:14 pm
by RobertGonzalez
Is the mod_rewrite module being parsed? That should be the first step. If you have a mod_rewrite module loaded, and the configuration for apache is correct, then it comes down to your rules. If that is the case, I would use a very simple starting rule and mess with that. I would also look at whether your clean url's end in a slash '/' or not, because sometimes that slash is interpreted as part of the URL wording and throws off what you are trying to do as well.

Posted: Mon Mar 05, 2007 4:44 pm
by Bill_VA
OK, here's a stupid question. How do I know if mod_rewrite is even loaded or installed? I don't see it anywhere in the httpd.conf file. I also can't find the mod_rewrite.so file anywhere.

Posted: Mon Mar 05, 2007 4:56 pm
by RobertGonzalez
That could be an indication that it is not loading. Are you on a Windows or *nix machine?

Posted: Mon Mar 05, 2007 4:57 pm
by RobertGonzalez
Because this thread is dealing mainly with mod_rewrite, I am moving this to the Installation and Configuration section until it starts talking about PHP again.

Posted: Mon Mar 05, 2007 5:14 pm
by Bill_VA
Thanks Robert!

Posted: Mon Mar 05, 2007 5:26 pm
by RobertGonzalez
Is it working? If so what did you do? If not, where do you stand on getting it going?

Posted: Mon Mar 05, 2007 5:43 pm
by Bill_VA
Actually, I was just thanking you for moving the posting. I'm still out of commission.

It's a Linux (Suse 10) box.

Posted: Mon Mar 05, 2007 5:49 pm
by Christopher
I have, over time, whittled my rewrite rules down to this:

Code: Select all

RewriteEngine on
RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php
So anything that does not have a 2-4 character extension is routed, but I use a Front Controller.