views:

124

answers:

2

Hello All,

I am wanting to rewrite a url like:
http://my.project/mydomain.com/ANY_NUMBER_OF_CATEGORIES/designer/4/designer-name/page.html
to this:
http://my.projects/mydomain.com/ANY_NUMBER_OF_CATEGORIES/page.html?designer=4

I would like to use mod-rewrite to accomplish this.

Things to note:

  1. Any number of categories can be between 'mydomain.com/' and '/designer'.
    1. For instance the url could be http://my.project/mydomain.com/designer/4/designer-name/page.html or it could be http://my.project/mydomain.com/tops/shirts/small/designer/4/designer-name/page.html
  2. A query string may be provided in the original url that needs to be preserved in the rewritten url.
    1. For example url provided could be: http://my.project/mydomain.com/designer/4/designer-name/page.html?color=red&type=shirt
    2. Given the url above the resulting url would need to be: http://my.projects/mydomain.com/page.html?designer=4&color=red&type=shirt
    3. The order of the query string does not matter. The 'designer=4' part could come before or after the rest of the query string.

I'm new to .htaccess and re-writes so any examples and or explanations would be greatly appreciated. Thank you very much.

+1  A: 

Try this in your .htaccess file:

RewriteEngine on
RewriteRule ^(.+/)?(\d+)/[^/]+/([^/]+\.html)$ $1$3?designer=$2 [L,QSA]
Gumbo
This looks good and it looks like it almost does it, however the "designer" part of the url stays in the rewrite.Result----http://my.project/mydomain.com/designer/4/designer-name/page.htmlbecomeshttp://my.projects/mydomain.com/designer/page.html?designer=4Though I was hoping it would be:http://my.projects/mydomain.com/page.html?designer=4I did do something in my .htaccess file that appears to be working - but being a "noob" I'm not sure how good it is:RewriteRule ^designer/([^/]+)/.*/([^/]+)\.html $2.html?designer=$1 [NC,L]I do have to add two almost duplicate rules this way though.
jeremysawesome
A: 

I ended up having to create two separate RewriteRules:

RewriteRule ^designer/([^/]+)/.*/([^/]+)\.html $2.html?designer=$1 [NC,L,QSA]
RewriteRule (.*)/designer/([^/]+)/.*/([^/]+)\.html $1/$3.html?designer=$2 [NC,L,QSA]

Thanks to Gumbo for pointing out the [QSA] portion of the rewrite.

jeremysawesome