Clean URLs for a PHP rookie
Moderator: General Moderators
Clean URLs for a PHP rookie
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.
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.
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
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:
As for mod_rewrite.... edit the .htacces file in your public_html or equivlent folder and add 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
?>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]feyd | Please use
I now have a .htaccess file in the root of my folder /srv/www/htdocs/mysite/.htaccess which reads:
my index.php file has:
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]
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 AllCode: 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]Code: Select all
<?php
$city = $_GET['city'];
$page= $_GET['page'];
$function= $_GET['function'];
print "here are the vars: $city, $page, $function";
?>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]- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
In your local .htaccess file, before the index.php instruction, prepend it with /mysite, so it looks like
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.
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]- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I have, over time, whittled my rewrite rules down to this:
So anything that does not have a 2-4 character extension is routed, but I use a Front Controller.
Code: Select all
RewriteEngine on
RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php(#10850)