Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite to text/type/id

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.


1 Answers

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 for rewrite.php?path=roberts-clothing-store/store/12/3 that will decode the URL and sets the arguments (here storeid and page) 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 master mod_rewrite.

The main prob could be with the overhead that might be significant but is the price of simplicity & flexibility. mod_rewrite is much faster

Update:

The other posts do answer the question, 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).

like image 173
Steve Schnepp Avatar answered Aug 10 '26 14:08

Steve Schnepp