Question

i created dynamic products like mini shopping cart my url something like this

<a class="button_view" href="view.php?procode=<?php echo $obj->product_code; ?>">View</a>

in browser i saw this link

http://localhost/sd/view.php?procode=PD1001

i want to rewrite url and make seo friendly my htacess is

<IfModule mod_rewrite.c>

Options +Indexes
Options         +FollowSymLinks
RewriteEngine   On
RewriteBase     /
RewriteCond %{QUERY_STRING} hack
RewriteCond %{HTTP_COOKIE} !go
RewriteRule . - [F]
RewriteRule ^view.php?procode=([a-zA-Z0-9]+)$ view/$1/$2
</IfModule>

i want something like this

http://localhost/sd/view/sunglasses/
http://localhost/sd/view/iphone6/
http://localhost/sd/view/androidphonefx1/
Was it helpful?

Solution

Change your href to:

<a href="/sd/view/<?php echo $obj->product_code.'/'.$obj->product_name; ?>">View</a>

then use these rules in your `/sd/.htaccess:

<IfModule mod_rewrite.c>
Options +Indexes
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /sd/

RewriteCond %{QUERY_STRING} hack
RewriteCond %{HTTP_COOKIE} !go
RewriteRule . - [F]

RewriteRule ^view/([\w-]+) view.php?procode=$1 [L,QSA,NC]
</IfModule>

OTHER TIPS

Ok, this sounds familiar to me.

Actually you have to make mod_rewrite to work on another direction.

You actually want to receive links like this:

http://yourhost/sd/view/sunglasses/{code}
http://yourhost/sd/view/iphone6/{code}

And you want to rewrite those internally to something like this:

http://yourhost/sd/view.php?procode={code}

So in that case your RewriteRule would be:

RewriteRule ^view/sd/[a-zA-Z0-9]+/([a-zA-Z0-9]+)$ view?procode=$1

Where you just need to grab your code from the url to pass to the rewritten procode param.

You can also grab both or just the item name and try to match the item just through the name in your system (what would be harder and maybe quite tricky).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top