Forum Moderators: phranque

Message Too Old, No Replies

Help with simplifing rewritecond in htaccess

same rewritecond for many rewriterule

         

popozsn

2:38 pm on May 5, 2011 (gmt 0)

10+ Year Member



Hi there,

I am new to this, so any advise and suggestions are more than welcome.

Here is my problem: Is there anyway I can simplifying my htaccess since I have mutiple same rewritebase and rewritecond for many rewriterule?

Here is my example:

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003011
RewriteRule ^12-345-m2003011-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003011
RewriteRule ^2003011\/(.*)-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003012
RewriteRule ^12-345-m2003012-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003012
RewriteRule ^2003012\/(.*)-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003013
RewriteRule ^12-345-m2003013-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

RewriteBase /document/12-345-m/
RewriteCond %{HTTP_REFERER} !^(.*)/otherDoc\.action(.*)$
RewriteCond %{HTTP_COOKIE} !12-345-m/2003013
RewriteRule ^2003013\/(.*)-(eng|chi)\.(.*)$ [mydomain.com...] [L,R=301]

... ... (There are many to follow with exactly same format but 2003014, 2003015 etc)

This is working right now, but it is getting too long with more doc added. How can I simply this, please?

Thanks a lot,
Popo

popozsn

12:34 pm on May 10, 2011 (gmt 0)

10+ Year Member



Could anybody help please?

g1smd

1:45 pm on May 10, 2011 (gmt 0)

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



Some syntax issues to address first.

Slashes do not need escaping:
\/
should be
/
only.

Whenever you see
(.*)
at the beginning or in the middle of a pattern it is the wrong thing to use. The
(.*)
reads all of the remaining pattern to the very end, then has to back off and retry hundreds or thousands of times.

Use a better pattern like
([^/]+)/
or
([^/]+/)+
here OR since the value you capture in the backreference are never used, simply omit the
^(.*)
and
(.*)$
parts.

In the middle of the code, you might need
([^-]+)-
too.

200301[0-9]
allows for the last digit to change.

20030[0-9]{2}
allows for the last two digits to change.

I don't know how you're going to make the comparison against the cookie value though.

Use example.com to stop forum auto-linking your URLs

popozsn

5:55 pm on May 10, 2011 (gmt 0)

10+ Year Member



Thank you so much for your answer and help. I really appreciate your time and help. I have remove the slashes and (.*) at the beginning of a pattern as you suggested. Here is what I have so far:

RewriteEngine on
RewriteBase /pub/12-345-m/

RewriteCond %{HTTP_REFERER} /otherDoc\.action(.*)$ [OR]
RewriteCond %{HTTP_COOKIE} 12-345-m/2009001
RewriteRule .* - [S=2]
RewriteRule ^12-345-m2009001-(eng|fra)\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$ [mydomain...] [L,R=301]
RewriteRule ^2009001/(.*)-(eng|fra)\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$ [mydomain...] [L,R=301]

RewriteCond %{HTTP_REFERER} !/otherDoc\.action(.*)$
RewriteCond $1 !\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$
RewriteRule ^12-345-m2009001-(.*)$ [mydomain...] [L,R=301]

RewriteCond %{HTTP_REFERER} !/otherDoc\.action(.*)$
RewriteCond $1 !\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$
RewriteRule ^2009001/(.*)$ [mydomain...] [L,R=301]

RewriteCond %{HTTP_REFERER} /otherDoc\.action(.*)$ [OR]
RewriteCond %{HTTP_COOKIE} 12-345-m/2008001
RewriteRule .* - [S=2]
RewriteRule ^12-345-m2008001-(eng|fra)\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$ [mydomain...] [L,R=301]
RewriteRule ^2008001/(.*)-(eng|fra)\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$ [mydomain...] [L,R=301]

RewriteCond %{HTTP_REFERER} !/otherDoc\.action(.*)$
RewriteCond $1 !\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$
RewriteRule ^12-345-m2008001-(.*)$ [mydomain...] [L,R=301]

RewriteCond %{HTTP_REFERER} !/otherDoc\.action(.*)$
RewriteCond $1 !\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$
RewriteRule ^2008001/(.*)$ [mydomain...] [L,R=301]


I read some suggestions in this forum and was able to shorten some rewrite with [s=2] and it works. But when I tried to combine:

RewriteCond %{HTTP_REFERER} !/otherDoc\.action(.*)$
RewriteCond $1 !\.(jpg|JPG|gif|GIF|htm|HTM|html|HTML)$

and try to use [s=2] the same as the first group, for some reason, it doesn't work. Could you please help me take a look? Thank you so much!

g1smd

6:02 pm on May 10, 2011 (gmt 0)

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



jpg|JPG|gif|GIF|htm|HTM|html|HTML
simplifies to
jpe?g|gif|html?
when using the
[NC]
flag.


Use example.com to stop forum auto-linking your URLs.