views:

156

answers:

2

I've got a url type:- http://www.example.com/products.php?cat=1 which I am able to rewrite to:- http://www.example.com/myproduct1 using the following .htaccess rule:- RewriteRule ^myproduct1$ products.php?cat=1.

What rule would I need to enter if I wanted to rewrite to:- http://www.example.com/myfolder1/myproduct1/?

A: 

For any arbitrary path segments a and b in /a/b/, you can use this:

RewriteRule ^([^/]+)/([^/]+)/$ products.php?segment1=$1&segment2=$2

Edit    In response to your comment: Just put good/boy in your pattern:

RewriteRule ^good/boy$ products.php?cat=1
Gumbo
The current links do not have any second segments. I want products.php?cat=1 to be rewritten to something like:- /good/boy, but currently I am only able to rewrite it to:- /boy.
nitbuntu
I did try ^good/boy$ products.php?cat=1, but the resulting page does not seem to recognize the css file. with /boy, it displays fine but with good/boy the page is rendered as though there was no css file.
nitbuntu
@nitbuntu: You just need to reference your CSS file correctly as now your base URL has changed. Try an absolute path (e.g. `/style.css`) instead of a relative one (e.g. `style.css` or `./style.css`).
Gumbo
@nitbuntu If you use URLs like /a/b/c, you need to take the rewritten URL into account in your links. If you use the same HTML (e.g. via a template) for different steps in the hierarchy, your best bet is either absolute URLs or the base element (which tells the browser to interpret relative URLs as relative to the specified context rather than the current URL).
Alan
I'd be interested to know what this base element is? Is it a script that goes into htaccess?
nitbuntu
@nitbuntu: The resource the reference is in defines the base URL. And since your reference is in `/good/boy`, `/good/boy` is also your base URL path (if not declared otherwise).
Gumbo
A: 

Hi,

First thing, I assume you want something like http://www.example.com/myproduct2 redirect to http://www.example.com/products.php?cat=2. In this case youd' rather use that rule:

RewriteRule ^myproduct([0-9])$ products.php?cat=$1

Then you can do th same this for your folder, writing something like:

RewriteRule ^myfolder([0-9])/myproduct([0-9])$ products.php?folder=$1&cat=$2

Edit

Then you just have to write:

RewriteRule ^folder1/myproduct1$ products.php?cat=1
RewriteRule ^myfolder/differentproduct$ products.php?cat=2

and so on...

gregseth
Actually I want to rewrite http://www.example.com/products.php?cat=2 to:- http://www.example.com/myfolder/differentproduct/. Currently I am able to get it to http://www.example.com/differentproduct
nitbuntu
There is no relationship between 'cat=1' and folder1
nitbuntu