My current code is something like this
store.php?storeid=12&page=3
and I'm looking to translate it to something like this
mysite.com/roberts-clothing-store/store/12/3
and something like this:
profile.php?userid=19
to
mysite.com/robert-ashcroft/user/19
I understand that it's best to have the SEO-friendly text as far left as possible, ie not
mysite.com/user/19/robert-ashcroft
(what stackoverflow does)
I can't figure out how to do this in apache's mod_rewrite. Any help?
Then you can just use RewriteRule directive in a .htacces like:
See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for help, or google.
Try these rules:
But I wouldn’t use such URLs. They don’t make sense when you think of the URL path as a hierarchy and the path segments as their levels.
Actually, you may have to think "upside-down" with
mod_rewrite
.The easiest way is that to make your PHP emit the rewritten
mysite.com/roberts-clothing-store/store/12/3
links.mod_rewrite
will then proxy the request to one PHP page forrewrite.php?path=roberts-clothing-store/store/12/3
that will decode the URL and sets the arguments (herestoreid
andpage
) and dynamically include the correct PHP file, or just emit 301 for renamed pages.A pure solution with
mod_rewrite
is possible, but this one is much easier to get right, especially when you don't mastermod_rewrite
.The main prob could be with the overhead that might be significant but is the price of simplicity & flexibility.
mod_rewrite
is much fasterUpdate :
The other posts do answer the questionn, but they don't solve the typical duplicate-content problem that avoided by having canonical urls (and using 301/404 for all those URLs that seems ok, but aren't).
My approach is to make the .htaccess as easy as possible and to do all the hard work in PHP:
This basically means to take everything and reroute it to my index.php file (in css/javascript/image directories I simply use "RewriteEngine off" to grand access to these files). In PHP I than just split("/", $param, 5) the string and run a foreach() to check all the parameters. Encapsulated in a nice function this works fine for me.
Update:
For this easy case I highly recommend the use of explode() instead of using split(), because explode() doesn't come with the overhead by using regular expressions.