dflow Posted March 26, 2010 Share Posted March 26, 2010 i want to create friendly urls like this example.com/CountryName/CityName my url looks like this: example.com/page.php?CountryID=12&CityName=23 whats the best approach ? Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/ Share on other sites More sharing options...
trq Posted March 26, 2010 Share Posted March 26, 2010 There should be plenty of examples within this board, have you had a look around? Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031960 Share on other sites More sharing options...
jmajeremy Posted March 26, 2010 Share Posted March 26, 2010 Well, I think the simplest method is to just send all requests to the respective page, and let PHP to the parsing of the URI. You'll have to come up with a regex for that. In PHP you could do something like <?php $uri = $_SERVER['REQUEST_URI']; $params = explode("/", $uri); You now have all the parts of the URI in an array. It would look something like this: array { [0] => 'http:' [1] => '' [2] => 'example.com' [3] => 'CountryName' [4] => 'CityName' } You can see how with the knowledge that REQUEST_URI will always contain what the user actually requested, and not the real path to the script, you can parse the URI and get the information you want out of it. Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031963 Share on other sites More sharing options...
dflow Posted March 26, 2010 Author Share Posted March 26, 2010 There should be plenty of examples within this board, have you had a look around? i have actually looked havent found an answer for this changing the numeric id to the name Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031966 Share on other sites More sharing options...
premiso Posted March 26, 2010 Share Posted March 26, 2010 RewriteRule ^(.*)/(.*)/?$ page.php?CountryID=$1&CityName=$2 [L] Should work. But may not yeild the exact results you want, as anything even say example.com/sub/realdir/ will get redirected as well. Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031978 Share on other sites More sharing options...
dflow Posted March 26, 2010 Author Share Posted March 26, 2010 RewriteRule ^(.*)/(.*)/?$ page.php?CountryID=$1&CityName=$2 [L] Should work. But may not yeild the exact results you want, as anything even say example.com/sub/realdir/ will get redirected as well. cool this works i still have the CityID as a number so the url looks like this amsterdam-tourist-sites/1.html regarding seo is this a good move keeping the number? Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031994 Share on other sites More sharing options...
trq Posted March 26, 2010 Share Posted March 26, 2010 You'll probably want to revert to using descriptive (name) based values. This is as simple as changing all your queries to search by name instead of numbers. Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1031996 Share on other sites More sharing options...
dflow Posted March 26, 2010 Author Share Posted March 26, 2010 You'll probably want to revert to using descriptive (name) based values. This is as simple as changing all your queries to search by name instead of numbers. that's a pain Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1032001 Share on other sites More sharing options...
trq Posted March 26, 2010 Share Posted March 26, 2010 Yes, in your situation it would be, which is why these things should be developed as you go. Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1032014 Share on other sites More sharing options...
dflow Posted April 9, 2010 Author Share Posted April 9, 2010 this should be correct ,no?? RewriteRule ^city/([a-zA-Z0-9-]+)\.html /city.php?CityName=$1 [L] Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1039724 Share on other sites More sharing options...
dreamwest Posted April 11, 2010 Share Posted April 11, 2010 this should be correct ,no?? RewriteRule ^city/([a-zA-Z0-9-]+)\.html /city.php?CityName=$1 [L] RewriteRule ^city/(.*)\.html city.php?CityName=$1 [L,QSA] Link to comment https://forums.phpfreaks.com/topic/196551-mod-rewrite-and-php/#findComment-1039882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.