views:

45

answers:

3
+1  Q: 

MOD_REWRITE HELP!

I want to use mode rewrite to display the following: mydomain.com/Florida/Tampa/ instead of mydomain.com/place.php?state=Florida&city=Tampa

I've akready done this: (since I think it might make a difference!) mydomain.com/[name].html instead of mydomain.com/profile?user=[name]

Here is the code!

Options +FollowSymLinks  
Options +Indexes 
RewriteEngine On 

RewriteBase /  
RewriteCond %{SCRIPT_FILENAME}! !-f  
RewriteCond %{SCRIPT_FILENAME}! !-d  
RewriteRule (.*).html profile.php?user=$1 [QSA.L]
A: 

Add:

RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/ place.php?state=$1&city=$2 [L,QSA]
webdestroya
This looks like it should work so long as there are no spaces, but St. Louis, Saint Petersburg, East Lansing, et. al. will not match. If the PHP code can handle it, you could use ^([^/]+)/([^/]+)
Devin Ceartas
Yea. He didn't give much insight into his possible values, I figured he might do something like StLouis/StPetersburg, etc
webdestroya
+2  A: 

You should make your patterns as specific as possible. So try these rules:

# stop rewriting process if request can be mapped to file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# get user pages
RewriteRule ^([^/]+)\.html$ profile.php?user=$1 [L,QSA]

# get paces
RewriteRule ^([^/]+)/([^/]+)/$ place.php?state=$1&city=$2 [L,QSA]

Here I use [^/]+ (one or more arbitrary characters except /). But if you only want to allow specific characters, you should reflect that in your patterns (see for example webdestroya’s proposal).

And you should also make sure that you use unambiguous URIs. You should develop a well-wrought URI structure before thinking about rules. You know, Cool URIs don’t change.

Gumbo
I like this solution, but, I would also suggest that having a standardised start for the URI would be advantageous.For instance, having all Userpages start with `/user/`, all Places start with `/place/`, etc. Allows for more extensibility down the track, rather than relying on the number of elements in the URI.
Lucanos
jsd911
write your general rules in httpd.ini, (if http.ini is empty write them in apache2.ini)with apache2 httpd.ini is empty because it's got so big over the years , any way.... Unlike the main server configuration files like httpd.conf, Htaccess files are read on every request therefore Apache searches all directories and subdirectories that are htaccess-enabled for an .htaccess file which results in performance loss due to file accesses. Keep in mind you have to restart apache if you do this.If you have a linux box: /etc/init.d/apache2 restart
Neo
A: 

i put Gumbo's version and everything works fine but I sometime the user selects a state before the city and right now domain.com/Florida/Tampa works but domain.com/Floria doesn't work!

jsd911