mod_rewrite subdirectories as variable
I am trying to rewrite "/products/MFG/date/SKU.html" to "MFG-SKU.html"
Our current rewrite below only rewrites to "SKU.html".
RewriteCond %{REQUEST_URI} ^/products/
RewriteRule /([^/]*)$ /$1 [R=301,NC,L]
How can I access MFG as a variable to include before the SKU?
An example URL would be:
http://example.com/products/MF开发者_StackOverflow中文版G/2010-04-01/1234.html => http://example.com/MFG-1234.html
MFG and 1234 are variables that would change.
Well, this one should do the job for you:
RewriteRule ^products/([^/]+)/[0-9-]+/([^/]+)\.html$ $1-$2.html [R=301,L]
It will redirect
/products/MFG/2010-04-01/1234.html
to/MFG-1234.html
.It does not validate the parameters (MFG, SKU, date):
- The
MFG
part can be anything within 1 path segment (which means any character except slash/
): can be letters, digits, Unicode chars etc. If you need to limit it -- just modify the pattern([^/]+)
to whatever restrictions you want to have; - The
date
part has very simple validation -- only digits and-
is allowed. So even123456-09876
will work. Once again -- if you need to have more precise validation just chnage the pattern; - The
SKU
part --same validation in place as forMFG
part.
- The
This rule is intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
精彩评论