i have a property website where alot of properties have been uploaded, presently the url of a property looks like this
http://propertyonline.com.ng/property.php?product_id=255
But for the sake of google optimization i know it should not remain like this so i am try to rewrite my url to something clean like the title of the property.
so instead of e.g
http://propertyonline.com.ng/property.php?product_id=255
we will have
http://propertyonline.com.ng/3 Bedroom Flat Large Compound On Banana Island Ikoyi
i have a variable that presently echos the title of the property when on this page dynamically based on the id of the property as above
<?php echo $row_prop['title']; ?>
Please how can i rewrite the url dynamically using htaccess
thanks
One approach you can format the URL as:
http://propertyonline.com.ng/{product_id}-{product_slug}
{product_id}
is numeric value, and {product_slug}
can be any string followed by the product id. Then you can use this RewriteRule
. $1
is back-reference to the grouped part ([0-9]+)
, represented to {product_id}
. [^/]*
matches any string except the forward slash character, represented to {product_slug}
.
RewriteRule ^([0-9]+)-[^/]* /property.php?product_id=$1 [QSA,L]
If you want clean url use product slug For slug use below code which will remove spaces and unwanted chars from string
function to_prety_url($str)
{
if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') )
$str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str));
$str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
$str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\1', $str);
$str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
$str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str);
$str = strtolower( trim($str, '-') );
return $str;
}
change your .htaccess code (or add)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/$ index.php [L]
#this is for product section
RewriteRule ^property/([A-Za-z0-9-]+)/?$ property.php?slug=$1 [NC,L]
ErrorDocument 404 http://www.akshadainfosystem.com/
RewriteRule ^([^\.]+)$ $1.php [NC,L]