Solved

htaccess Rewrite Rule

Posted on 2011-02-16
7
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

717 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