Forum Moderators: phranque
Cant figure how to make this work.
I need to redirect about 250 products pages that my shopping cart generate on its search page to the actual static pages that I have on my website.
So I will need to hard code all of them but I just need the code:)
The url I need to rewrite looks like this:
http://www.example.com/Merchant2/merchant.mvc?Screen=PROD&Product_Code=DSM-6117&Category_Code=
What I want to do is to redirect to a static page that I already have on the server every url that has "Screen=PROD" and a product code(in this case its DSM-6117). The product code is the only string that changes in the url. The "Screen=PROD" will only be in a url I need to redirect.
I know I can match the whole url and redirect it to the static page but I'm trying to find a way to make the file smaller.
Any help will be appreciated
Thank You.
Thanks,
Jim
So now I have this:
To redirect
http://www.example.com/Merchant2/merchant.mvc?Screen=PROD&Product_Code=DSM-6116&Category_Code=
to http://www.example.com/dir/page.htm
I use:
RewriteCond %{REQUEST_URI} ^/Merchant2/merchant.mvc$ [NC]
RewriteCond %{QUERY_STRING} ^Screen=PROD&Product_Code=DSM-6116(.+)
RewriteRule ^.*$ http://www.example.com/dir/page.htm? [R=301,L]
And its working fine, however since I'm going to use about 250 redirects I am looking for a way to make the code smaller so it will run faster.
Any suggestion?
There's also no need for the "(.+)" at the end of the second RewriteCond's pattern. If you wish to require more characters following "6116", then a single "." will do, since there is no end-anchor. Or you could be more specific and require the additional character to be numeric, or alphabetic, or an ampersand -- whatever is appropriate.
Jim
Move the REQUEST_URI pattern (sans the leading slash if your code is in .htaccess) to the RewriteRule, and eliminate that first RewriteCond.
I thought I will need to use the REQUEST_URI only once in my htaccess file followed by these 250 RewriteRules. Am I wrong?
And also I was wondering maybe I'm just wasting time on trying to make the file shorter, Do I need to be worried about the size of that file if its going to contain around 250 RewriteRules?
You can use a "quit" or "skip" function if necessary:
# Quit if NOT a request for Merchant2/merchant.mvc
RewriteRule !^Merchant2/merchant\.mvc$ - [L]
# Skip next 250 rules if NOT a request for Merchant2/merchant.mvc
RewriteRule !^Merchant2/merchant\.mvc$ - [S=250]
RewriteCond dir/page1.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6116 [OR]
RewriteCond dir/page2.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6117 [OR]
RewriteCond dir/page3.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6118 [OR]
RewriteCond dir/page4.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6119 [OR]
RewriteCond dir2/page1.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6120 [OR]
RewriteCond dir2/page2.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6121
RewriteRule ^Merchant2/merchant\.mvc$ http://www.example.com/%1? [R=301,L]
Jim
RewriteCond dir/page1.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6116 [OR]
RewriteCond dir/page2.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6117 [OR]
RewriteCond dir/page3.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6118 [OR]
RewriteCond dir/page4.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6119 [OR]
RewriteCond dir2/page1.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6120 [OR]
RewriteCond dir2/page2.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6121
RewriteRule ^Merchant2/merchant\.mvc$ http://www.example.com/%1? [R=301,L]
Jim, I tried figuring and implementing what you have suggested, and before I will experience brain damage, I came for your help again :)
Its not working for me and I cant understand whats the dir/page.htm>% before the {QUERY_STRING} is doing there.
The condition will not be met as the "dir/page.htm" belongs to the right side of the RewriteRule and will not show on the url that needs to be redirected.
I also thought thats its there only as a variable that is not checked as a condition but it seems that the RewriteCond is doing something with it.
This is what I have now:
The htaccess is in the Merchant2 dir.
RewriteCond premier/premier-6116.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6116 [OR]
RewriteCond premier/premier-6119.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6119 [OR]
RewriteCond premier/premier-6122.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6122 [OR]
RewriteCond premier/premier-6124.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-6124 [OR]
RewriteCond luxury/lux-3111.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-3111 [OR]
RewriteCond luxury/lux-3112.htm>%{QUERY_STRING} ^([^>]+)>Screen=PROD&Product_Code=DSM-3112
RewriteRule ^Merchant2/merchant\.mvc$ http://www.example.com/%1? [R=301,L]
If this code is in Merchant2/.htaccess, then remove the "Merchant2/" part of the URL-path from the RewriteRule pattern, as it won't be present:
RewriteRule ^merchant\.mvc$ http://www.example.com/%1? [R=301,L]
I only need to verify a code I wrote about url that I can rewrite using a common RewriteRule. This will save me about 100 redirect using the above method.
For example in my url the only unique query string is the DSM-ffff
and the ffff has a unique pattern meaning if it starts for example with 6 then it belongs to a category f & the page I want to redirect it to will look like category-ffff.htm
So I wrote this code:
RewriteCond %{QUERY_STRING} ^Screen=PROD&Product_Code=DSM-(6[^&]+)
RewriteRule ^merchant\.mvc$ http://www.example.com/category/category-%1\.htm? [R=301,L]
The code is working, do I need to fix anything?
Thanks
RewriteRule ^merchant\.mvc$ http://www.example.com/category/category-%[b]1.h[/b]tm? [R=301,L]
Jim