Solved

htaccess Rewrite Rule

Posted on 2011-02-16
7
411 Views
Last Modified: 2012-05-11
I have a site url
www.mysite.com/index.php?productid=6

The site goes into a database via php and selects the category, type and name based on the product id.

Category = widget
Type = Red
Name = Fluffy

How can I rewrite the url as

www.mysite.com/widget/Red/Fluffy

I believe the server etc is a standard LAMP configuration.

Thanks

Mike
0
Comment
Question by:hungoveragain
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:andreizz
ID: 34904571
Create a .htaccess file in your document root(where index.php or index.html is)
and in it put the following text:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /?category=$1&type=$2&name=$3 [L]

Open in new window


You can use this generator in the future: http://www.generateit.net/mod-rewrite/

Good luck!
0
 
LVL 3

Assisted Solution

by:QuinnDester
QuinnDester earned 250 total points
ID: 34904808
You can not shorten the URL down to www.mysite.com/widget/Red/Fluffy

you will need a URL like this  www.mysite.com/product/6/widget/Red/Fluffy.htm

You will need the /product/ in the url to identify that it is a product page for the rewrite URL to match to, so your rewrite rule will look like this.

RewriteEngine On
RewriteRule ^product/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+).htm$  index.php?product=$1&category=$2&type=$3&name=$4\.htm  [NC,L,U]

I have added the .htm on the end as without it search engines will index the page as a folder and it will get lower ranking than a page with the same SEO ranking

0
 

Author Comment

by:hungoveragain
ID: 34905819
Does this mean the original url will need to have &category, &type and &name in the query string or can these be referenced from the php?

The original query string looks like this

www.mysite.com/index.php?productid=6

The product will have an associated category, type and name but those will be picked up by the php from the database.

Thanks

Mike
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Accepted Solution

by:
andreizz earned 250 total points
ID: 34906008
QuinnDester:
You can not shorten the URL down to www.mysite.com/widget/Red/Fluffy

you will need a URL like this  www.mysite.com/product/6/widget/Red/Fluffy.htm

You will need the /product/ in the url to identify that it is a product page for the rewrite URL to match to, so your rewrite rule will look like this.

RewriteEngine On
RewriteRule ^product/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+).htm$  index.php?product=$1&category=$2&type=$3&name=$4\.htm  [NC,L,U]

I have added the .htm on the end as without it search engines will index the page as a folder and it will get lower ranking than a page with the same SEO ranking

If you take what he wrote above, and change it to:
RewriteEngine On
RewriteRule ^product/([^/.]+)/[^/.]+/[^/.]+/[^/.]+.html$  index.php?productid=$1\.html  [NC,L,U]

Open in new window


This will take a url like www.mysite.com/product/6/widget/Red/Fluffy.html and channel it to www.mysite.com/index.php?productid=6 so you would generate from your code urls like the one with .html kinda like a fake url(only the /6/ will be taken in account by mod_rewrite the rest is just for display).
0
 
LVL 3

Expert Comment

by:QuinnDester
ID: 34906239
I was assuming the page would utelise the widget/Red/Fluffy

IE by displaying the correct image for the red fluffy widget as opposed to the blue smooth widget or/and using the terms red fluffy widget in meta tags and page text to increase SEO

if so you will need to use the url syntax i have given so you can pick up the terms from the url and add them to varibles to be used in the page..

if they are just for show then the syntax andreizz: gave will work fine
0
 
LVL 3

Expert Comment

by:QuinnDester
ID: 34906305
the adding of extra parameters on the url will not affect the running of the site, your will not even need to add them in the page links, this will be in the rerwrite only, they will be dummy parameters, allowing you to pick them up with code (in PHP)

<?php thisCategory = $_GET["category"]; ?>

you can then use this in your page and meta tags
0
 

Author Comment

by:hungoveragain
ID: 34907288
That is extremely helpful - thanks.

Mike
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now