Jump to content

[SOLVED] Beginners problem


StefanRSA

Recommended Posts

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  :-\

Link to comment
https://forums.phpfreaks.com/topic/180972-solved-beginners-problem/
Share on other sites

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;
}
?>

 

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?

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.