Solved

htaccess Rewrite Rule

Posted on 2011-02-16
7
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Accepted Solution

by:
andreizz earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That is extremely helpful - thanks.

Mike
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

10 Experts available now in Live!

Get 1:1 Help Now