Forum Moderators: phranque

Message Too Old, No Replies

Need help with htaccess Pls

         

asher02

12:20 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi all,

After a lot of studying I have found a solution to my problem, however I would like to ask if there is a simple way or shorter way to achieve the same goals.

I need to remove the specific query strings found in my ataccess from specific URLs

so my approach was to deal with each one on separate command:
I'm asking if its OK and if there is a better way to do it?

Thanks,

My ataccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mysite.com
RewriteRule (.*) [mysite.com...] [R=301,L]

RewriteCond %{QUERY_STRING} source=mezuzah [NC]
rewriterule ^mezuzah\.htm /mezuzah.htm? [R=301]

RewriteCond %{QUERY_STRING} source=tallit [NC]
rewriterule ^tallis\.htm /tallis.htm? [R=301]

RewriteCond %{QUERY_STRING} source=matzah [NC]
rewriterule ^matzah-cover\.htm /matzah-cover.htm? [R=301]

RewriteCond %{QUERY_STRING} ^source=syas$
RewriteRule ^$ [mysite.com...] [R=301,L]

jdMorgan

2:19 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ready for a headache? OK, here goes:

You can combine variables and patterns in mod_rewrite directives, and this can be used to accomplish your goal:


RewriteCond %{REQUEST_URI}<>%{QUERY_STRING} ^(/mezuzah\.htm)<>([^&]+&)*source=mezuzah&?¦(/tallis\.htm)<>([^&]+&)*source=tallit&?¦(/matzah-cover\.htm)<>([^&]+&)*source=matzah&?¦(/)<>([^&]+&)*source=syas$) [NC]
RewriteRule ^([^.]+\.htm)?$ http://www.example.com%1? [R=301,L]

Note that the "<>" character sequence is arbitrary; It is used only as a "boundary marker" allowing us to distinguish the end of the URL from the beginning of the query string in each pattern. It has no special meaning in regular expressions.

Now if the above method gets too long and ugly (if you have to add any more URL/query combinations), you can break it into multiple lines:


RewriteCond %{REQUEST_URI}<>%{QUERY_STRING} ^(/mezuzah\.htm)<>([^&]+&)*source=mezuzah&? [NC,OR]
RewriteCond %{REQUEST_URI}<>%{QUERY_STRING} ^(/tallis\.htm)<>([^&]+&)*source=tallit&? [NC,OR]
RewriteCond %{REQUEST_URI}<>%{QUERY_STRING} ^(/matzah-cover\.htm)<>([^&]+&)*source=matzah&? [NC,OR]
RewriteCond %{REQUEST_URI}<>%{QUERY_STRING} ^(/)<>([^&]+&)*source=syas$
RewriteRule ^([^.]+\.htm)?$ http://www.example.com%1? [R=301,L]

The extra "([^&]+&)*" is needed to allow the "source=" parameter to 'float' anywhere within the query string and still match. It simply says that mod_rewrite should look for any number (including zero) of "<something>&" sequences preceding "source=". You could do with just "(.+&)*" but the pattern I used is more efficiently-processed because it's less ambiguous.

Note that the final RewriteCond must NOT have an [OR] on it!

I have retained the end-anchor you specified for "source=syas". I assume you only want to redirect if the query ends with this string. Otherwise, remove the "$" from the end of "syas" and add the "&?" as in the others.

Apologies for any errors -- Not tested and typed fast!

Change all broken pipe "¦" characters above to solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

asher02

5:21 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Thanks Jim,

I really appreciate your help:)

One questions, what are the benefits of your method compering to the one I made?

jdMorgan

3:00 am on Mar 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code is one-half the original size, basically, and possibly easier to maintain.

Jim