Mod_rewrite()

He has: 6 posts

Joined: Aug 2005

Well, I'm new in this 'Swiss Army Knife' in rewriting.
Basically I would like to rewrite url :
http://mydomain.com/product/1/ that is actually is http://mydomain.com/product/?genId=1

this is what i wrote, in .htaccess that is located under /site/virtual/11/www/ :

RewriteEngine On
RewriteRule ^/product/(1-9)+/$ ^/product/?genId=$1

The page always display an 404 page..

But when I change to

RewriteEngine On
RewriteRule ^product/(1-9)+/$ ^product/?genId=$1

Note: without slash in ^product..

The rewrite works fine.

But I see almost all mod_rewrite tutorial using :
RewriteRule ^/product/(1-9)+/$ ^/product/?genId=$1
With slash between ^ and product.
So .. I was curious, which is correct ?
With slash or without?

Busy's picture

He has: 6,151 posts

Joined: May 2001

Like everything there is more than one way to do this.

The first slash can force to root level but all your doing is changing the file in a folder, not the file in main folder.
would be easier IMO to do: mydomain.com/product.php?genId=1 which would be mydomain.com/product1
don't understand why people use folders galore, is more work

He has: 6 posts

Joined: Aug 2005

So, in other words, my .htaccess is correct to this context?

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

All that the / does is to force it back to your document root. So links like this
/index.php index.php would both lead to the same place as long as your were in the root directory. If you were in a sub-directory, it would be different because the / would force it to look for the file in the document root, where the non-slashed version would cause it to look for the file in the sub-directory. The / version should work but it depends on where the mod_rewrite file is. Is it in the document root or in a sub-folder?

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.