Hey Everyone, Hoping someone can help me out, I have searched everywhere and found a few bits and pieces but can't seem to put it together. I am trying to create 2 variables based on a folder structure - city and state really. Like this: http://www.website.com/state/city/ Then on the page I want to create 2 variables: $state = folder1 $city = folder2 Is there an easy way to get this info? The folders won't really exist so I will need some sort of htaccess wildcard for them, but I am mostly concerned with the PHP part. Anyone have any ideas? Thanks very much!!
the php part is depends on how the htaccess handle the query. if the htaccess is like this : Options +FollowSymLinks RewriteEngine On RewriteRule ^/(.*)/(.*)$ index.php?state=$1&city=$2 [L] Code (markup): so the php part will be like this : $state = $_GET['state']; $city = $_GET['city']; PHP:
Hey Hangbowl, I was wondering if I mostly needed the htaccess, I just found a post that looks like I might be able to do most of it in the .htaccess, Really all of the files will be in root, I just want the 2 folders for SEO reasons, if I created a link like above (www.website.com/state/city/index.php?state=$1&city=$2) would it just grab the index file from root and the I use the GET (as you stated above) to display the city and state on page? Thanks again for your help!
if the url is like this : http://www.website.com/west-state/current-city/ so it means in your php file the value of state = west-state city = current-city
I was playing with your code, it doesn't seem to be working, I just get a 404 error? I really just want to make a link (fake) www.website.com/state/city/ and the use the PHP to get the 2 folders. It will just use index.php in the root.
404 error means the htaccess is not correct. try this RewriteRule ^/(.*)/(.*)$ /index.php?state=$1&city=$2 [L] Code (markup):
Still 404, I am searching stack exchange and stuff for answers as well, I think your on the right track though!
Yes, the logic is like that. try this RewriteRule ^/(.*)/(.*)/?$ /index.php?state=$1&city=$2 [L] Code (markup):
Hey Hangbowl, Got it working with this: RewriteRule ^(.*)/(.*)/?$ /index.php?state=$1&city=$2 [L] Only issue is state has a trailing / lol Any easy fix for that? It displays state/ and city as the variables lol
I hope this will work, make another htaccess above that line. RewriteRule ^(.*)?$ /index.php?state=$1 [L] Code (markup):
That just broke it lol and I realized it is not adding the / to state, it is combining all of it so the state variable creates: state/city and the city variable does nothing
Got it!! RewriteRule ^(.*)/(.*)/ /index.php?state=$1&city=$2 [L] Creates a city and state variable!! Thanks so much for your help you saved me days of creating stupid folders!!
Hey @hangbowl thanks again for your help yesterday! I was wondering if you might have a little more insight to share?? I have the city state working, but I have another folder level for an article, so www.website.com/state/city/article-title/ in the .htaccess I created this (the first 3 lines are your stuff): Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)/(.*)/ index.php?state=$1&city=$2 RewriteRule ^(.*)/(.*)/post-title/ post.php?state=$1&city=$2 the 4th line however works, but it shows the page index.php as the state, city works great - am I missing something? I assume the logic in line 3 is overriding line 4. if I get rid of line 3 4 works lol Thanks again for your help!!