Forum Moderators: phranque

Message Too Old, No Replies

Improve redirection rules

         

Jake1234

3:14 pm on May 4, 2012 (gmt 0)

10+ Year Member



I am using certain redirection rules in httpd.conf and they work like a charm. However, I am not very content with the way it is. I am certain that there must be efficient and better ways to do this.

Therefore, I would like to know how I could improve the following piece of code for better performance. I want to avoid repetition (something like or condition maybe helpful)

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/blogs/([A-W0-9a-f]{13}_([0-9a-f]{4}_){2}[0-9a-f]{12})$
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/blogs/([A-W0-9a-f]{13}_([0-9a-f]{4}_){2}[0-9a-f]{12})$
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/forums/html/topic
RewriteCond %{QUERY_STRING} ^id=([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

--------------------------------------------------------------------
This is another snippet of the code I would like to improve

RewriteCond %{REQUEST_URI} !^/mobile
RewriteCond %{HTTP_USER_AGENT} "BlackBerry|Opera|Palm|Symbian|Android|iPhone|iPod" [NC]
RewriteCond %{REQUEST_URI} ^/homepage/login
RewriteRule .? [%{HTTP_HOST}%1...] [R=301,L]

lucy24

6:28 pm on May 4, 2012 (gmt 0)

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



Every one of your rules contains these two lines
RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$

If the first Condition fails, you will never get to the second Condition. And if the first Condition applies, the second Condition will always apply. Except for the added option of more than one slash /+ since the first Condition didn't allow for that possibility.

All the Rules have the same problem: the combination of .? in the Rule with something more explicit in the Condition. This means the server will use a colossal amount of time checking Conditions that could have been put in the Rule itself. For example:

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteRule ^blogs/([A-W0-9a-f]{13}_([0-9a-f]{4}_){2}[0-9a-f]{12})$ https://www.example.com/mobile/blogs/ShowBlogEntry?handle=$1 [R=301,L]


Note that {HTTP_HOST} in the Target is just plain wrong. At this point you should give one specific canonical form of your domain name, not stick with whatever the user happened to request in the first place (with/without www., with/without port number).

Matter of fact, the first four Rules seem to be the same:

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteRule ^blogs/(([A-W0-9a-f]{13}_|[0-9a-f]{8}-)(([0-9a-f]{4}_){2}|([0-9a-f]{4}-){3})[0-9a-f]{12})$ https://www.example.com/mobile/blogs/ShowBlogEntry?handle=$1 [R=301,L]


Except that I must be missing something, because I don't see where Rules 2 and 4 would apply, since they seem to be identical to Rules 1 and 3.

Deeper question: Why are you replacing one with-query form to another? Generally people don't bother redirecting requests in this form unless they're going to a pretty, request-less URL.

Do you have a CMS that accepts "ShowBlogEntry" (without extension or slash) as a filename? If not, you would need another Rewrite to deal with it.

g1smd

8:24 pm on May 4, 2012 (gmt 0)

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



Why are these rules configured as redirects? Shouldn't they all be internal rewrites?

^/blogs/+(.*)$
- what is the + for?
That's likely an error, as it allows a request like
example.com/blogs//////something
with multiple slashes to be a valid URL.

lucy24

2:42 am on May 5, 2012 (gmt 0)

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



it allows a request like example.com/blogs//////something with multiple slashes to be a valid URL

Happily the situation would never arise, since the Condition is preceded by a more narrowly defined one that specifies exactly what should come after the /blogs/ element.

btw, I tried it out on MAMP. Looks like the \h shorthand doesn't work-- no error, it simply isn't recognized. Otherwise you could use it instead of [0-9a-f] at a savings of six bytes :(