Forum Moderators: phranque
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]
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]
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]
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