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
Solved

htaccess Rewrite Rule

Posted on 2011-02-16
7
421 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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