Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

htaccess Rewrite Rule

Posted on 2011-02-16
7
Medium Priority
?
443 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Accepted Solution

by:
andreizz earned 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

610 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