StefanRSA Posted November 10, 2009 Share Posted November 10, 2009 Hi... I am a virgin when it comes to mod_rewrite so please bare with me... I have the following: RewriteRule ^vehicle/([0-9]+)/(.+)/(.+)$ /cars.php?catid=3&subcatid=$1&prov=$2&town=$3 [L] Its not working and I found the the problem is in the last two vars as it its even including the "/" for the second last var..... (.+)/(.+) The reason I want any character for prov and town is because both of them can have either Letters, space or a "-" like: cars.php?catid=3&subcatid=2&prov=prov name&town=town-name Can anybody please help me in simple language... (I am still very rusty with the high tec words used when people talk about mod_rewrite :-\ Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/ Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2009 Share Posted November 10, 2009 You should never allow any characters (.*) RewriteRule ^vehicle/([0-9]+)/([A-Za-z0-9- ]+)/([A-Za-z0-9- ]+)$ cars.php?catid=3&subcatid=$1&prov=$2&town=$3 [L] Use a function in your code to prepare any strings to be used in a url. Then you will know exactly what characters to expect in the url i.e <?php function prepareUrlText($string) { // no characters except a-z, 0-9, dash, underscore or space $blacklistChars = '#[^-a-zA-Z0-9_ ]#'; $string = preg_replace($blacklistChars, '', $string); $string = trim($string); $string = preg_replace('#[-_ ]+#', '-', $string); $string = strtolower($string); return $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954807 Share on other sites More sharing options...
StefanRSA Posted November 10, 2009 Author Share Posted November 10, 2009 Thanks Neil... Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954816 Share on other sites More sharing options...
StefanRSA Posted November 10, 2009 Author Share Posted November 10, 2009 Neil, I get Internal Server Error when I add your code? When I take it out all is fine again... Did I miss anything? Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954820 Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2009 Share Posted November 10, 2009 What code Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954831 Share on other sites More sharing options...
StefanRSA Posted November 10, 2009 Author Share Posted November 10, 2009 Sorry, when I add this to my .htaccess RewriteRule ^vehicle/([0-9]+)/([A-Za-z0-9- ]+)/([A-Za-z0-9- ]+)$ cars.php?catid=3&subcatid=$1&prov=$2&town=$3 [L] Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954837 Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2009 Share Posted November 10, 2009 Sorry I cannot see anything wrong with this line. The regex is perfectly valid. Try adding / prior to cars.php RewriteRule ^vehicle/([0-9]+)/([A-Za-z0-9- ]+)/([A-Za-z0-9- ]+)$ /cars.php?catid=3&subcatid=$1&prov=$2&town=$3 [L] Are you sure it is in the correct place? Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954842 Share on other sites More sharing options...
StefanRSA Posted November 10, 2009 Author Share Posted November 10, 2009 If I use it with the forbidden (.+) I get no error RewriteEngine On RewriteRule ^vehicles/([0-9]+)/(.+)$ /cars.php?catid=3&subcatid=$1&prov=$2 [L] RewriteRule ^vehicles_for_sale/([0-9]+)$ /cars.php?catid=3&subcatid=$1 [L] But when I use the same code and replace the forbidden (.+) with ([A-Za-z0-9- ]+) do I get an Internal server error RewriteEngine On RewriteRule ^vehicles/([0-9]+)/([A-Za-z0-9- ]+)$ /cars.php?catid=3&subcatid=$1&prov=$2 [L] RewriteRule ^vehicles_for_sale/([0-9]+)$ /cars.php?catid=3&subcatid=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954849 Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2009 Share Posted November 10, 2009 Ah shit. You cannot have spaces in a url. Replace with RewriteRule ^vehicle/([0-9]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ /cars.php?catid=3&subcatid=$1&prov=$2&town=$3 [L] Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954853 Share on other sites More sharing options...
StefanRSA Posted November 10, 2009 Author Share Posted November 10, 2009 Thanks, now its working but not with the space. Does this mean that I cannot use a space in the modrewrite and have to replace the space with a "-" ? Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954857 Share on other sites More sharing options...
JonnoTheDev Posted November 10, 2009 Share Posted November 10, 2009 correct. you cannot have spaces in a url. The php function that I have posted will clean strings to be url friendly. i.e The Rain In Spain *?&___ And Some would become the-rain-in-spain-and-some Quote Link to comment https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/#findComment-954920 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.