Forum Moderators: phranque

Message Too Old, No Replies

Need help with htaccess file please

         

asher02

9:07 pm on May 15, 2008 (gmt 0)

10+ Year Member



Hi all,

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.

jdMorgan

9:12 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See if this thread [webmasterworld.com] helps get you started, and please take a look at our forum charter for more information about mod_rewrite, regular expressions, and how to get the most from this forum.

Thanks,
Jim

asher02

10:23 am on May 16, 2008 (gmt 0)

10+ Year Member



Thank you Jim for the link, it gave me a good start point.

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?

jdMorgan

2:00 pm on May 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Move the REQUEST_URI pattern (sans the leading slash if your code is in .htaccess) to the RewriteRule, and eliminate that first RewriteCond.

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

asher02

9:01 pm on May 17, 2008 (gmt 0)

10+ Year Member



Thank you Jim for helping me.


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?

jdMorgan

10:13 pm on May 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteConds apply only to the single RewriteRule that they precede.

You can use a "quit" or "skip" function if necessary:


# Quit if NOT a request for Merchant2/merchant.mvc
RewriteRule !^Merchant2/merchant\.mvc$ - [L]

-or-

# Skip next 250 rules if NOT a request for Merchant2/merchant.mvc
RewriteRule !^Merchant2/merchant\.mvc$ - [S=250]

Or you can use a "RewriteCond multiplexer" rule:

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]

Note that the replacement URL-path corresponding to each query string is written on the left side of each RewriteCond, matched into a variable (%1) on the right side, and back-referenced in the RewriteRule. The ">" character is meaningless to regular-expressions, and serves only to demarcate the replacement URL-path from the query string for the regex matching engine.

Jim

g1smd

10:38 pm on May 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond affects only the next RewriteRule, not multiple rules.

You don't need the Cond if you can get the test pattern into the left-side of the Rule.

asher02

12:41 pm on May 18, 2008 (gmt 0)

10+ Year Member



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]

I'm lost:(

jdMorgan

1:45 am on May 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I explained how it works in my post above. The replacement URL is matched into the "([^>]+)" in the RewriteCond pattern, and is referred to as "%1" in the right side of the RewriteRule.

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]

Jim

asher02

5:50 pm on May 19, 2008 (gmt 0)

10+ Year Member



Thank you Jim now it works like charm :)

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

jdMorgan

6:04 pm on May 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The URL in the right side of a rewriterule is NOT a regular-expressions pattern. Therefore, it is not necessary to escape any characters except "$" and "%". You can delete the "\" before the "." in your URL:

RewriteRule ^merchant\.mvc$ http://www.example.com/category/category-%[b]1.h[/b]tm? [R=301,L]

Otherwise, looks OK to me, but test, test, test! :)

Jim