svgmx5 Posted May 26, 2011 Share Posted May 26, 2011 i'm looking for help with rewriting a url i have.... The link i have currently looks like this: domain.com/page.php?city=cityname&state=state&country=country I want to make that URL look like this: domain.com/countryname/statename/cityname/ I have included the current .httaccess that i'm using to covert other links that i have... <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ types.php?name=$1 [L,QSA] </IfModule> This is working to convert links such as this: 'domain.com/page.php?name=name' to 'domain.com/name' This work perfectly but since i have to have the city, state and country in the URL to display the correct city i want to know how i can use the .htaccess to create that. Hope someone can help me Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/ Share on other sites More sharing options...
phpJoeMo Posted May 26, 2011 Share Posted May 26, 2011 I think something along the lines of this will do what you want: Put the following ABOVE your rewrite rules & conditions: RewriteBase / Then add this anywhere in your rules & conditions: RewriteCond %{QUERY_STRING} ^city=([^/]*)&state=([^/]*)&country=([^/]*) RewriteRule ^.*$ /%1/%2/%3/ Let me know how it works out. Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1220432 Share on other sites More sharing options...
phpJoeMo Posted May 26, 2011 Share Posted May 26, 2011 Alternatively you could reverse the order like so: RewriteCond %{QUERY_STRING} ^city=([^/]*)&state=([^/]*)&country=([^/]*) RewriteRule ^.*$ [b] /%3/%2/%1/[/b] Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1220434 Share on other sites More sharing options...
phpJoeMo Posted May 26, 2011 Share Posted May 26, 2011 Sorry didn't realize the bold didn't work inside the code. You can alter the order like so: RewriteCond %{QUERY_STRING} ^city=([^/]*)&state=([^/]*)&country=([^/]*) RewriteRule ^.*$ /%3/%2/%1/ Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1220435 Share on other sites More sharing options...
svgmx5 Posted May 26, 2011 Author Share Posted May 26, 2011 Thanks, I should try out now! Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1220443 Share on other sites More sharing options...
svgmx5 Posted May 26, 2011 Author Share Posted May 26, 2011 well i tried it out and i'm getting this error on the screen [an error occurred while processing this directive] and here's the .htaccess code i have now: <IfModule mod_rewrite.c> RewriteBase / RewriteEngine on RewriteCond %{QUERY_STRING}^city=([^/]*)&state=([^/]*)&country=([^/]*) RewriteRule ^.*$/%1/%2/%3/ </IfModule> Now i'm trying to access it by typing it the following way: domain.com/locations/city/united_states/illinois/chicago also...previoulsy on the actual file which is under city/index.php i have php code that looks like this <?php $city = $_GET['city']; $state = $_GET['state']; $country = $_GET['country']; ?> Is there anything i should change there? Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1220461 Share on other sites More sharing options...
svgmx5 Posted May 29, 2011 Author Share Posted May 29, 2011 Okay, i found the answer to the problem after looking around thank's for the help though. Quote Link to comment https://forums.phpfreaks.com/topic/237483-rewrite-url/#findComment-1221905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.