Forum Moderators: phranque

Message Too Old, No Replies

Redirecting for both static and dynamic versions of the same url

My code works but seems inefficient

         

Merganser

4:57 am on Jan 31, 2012 (gmt 0)

10+ Year Member



I converted a few pages from dynamic to static urls (search engine friendly links). Then I realized I had previously implemented RewriteRules for certain malicious queries when the pages were dynamic. Now that the pages can be accessed either dynamically or statically, I feel I need to establish equivalent RewriteRules for the static pages. The code below is what I have come up with and it seems to work.

# Block bogus GET variables on page1.php, page2.php, and page3.php pages
# -- Handle when requested dynamically (e.g., page1.php?item=100)
RewriteCond %{QUERY_STRING} http:// [NC,OR]
RewriteCond %{QUERY_STRING} ftp:// [NC,OR]
RewriteCond %{QUERY_STRING} (\?|%3F) [NC,OR]
RewriteCond %{QUERY_STRING} trackback [NC,OR]
RewriteRule ^(page1\.php|page2\.php|page3\.php) - [F]
# -- Handle when requested statically (e.g., page1/100)
RewriteCond $2 http:// [NC,OR]
RewriteCond $2 ftp:// [NC,OR]
RewriteCond $2 (\?|%3F) [NC,OR]
RewriteCond $2 trackback [NC]
RewriteRule ^(page1|page2|page3)/(.*) - [F]

However, my real list of conditions is longer and I view it as a bit inefficient to list each condition twice (once for static and once for dynamic). Seems like there would be a better way. Any suggestions or improvements would be greatly appreciated.

lucy24

5:26 am on Jan 31, 2012 (gmt 0)

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



Tangential answer: Y'know you don't really need all that $2 stuff. It can go straight into the Rule itself as

RewriteRule ^(page1|page2|page3)/((ht|f)tp://|\?|%3F|trackback) - [F]

But the real question is: how are those malicious queries turning into static URLs in the first place?

Now that the pages can be accessed either dynamically or statically

Hm, well, they shouldn't. People using the old dynamic URLs should be redirected to static URLs. And then all those static URLs-- whether originally requested, or the result of a redirect-- get rewritten to behind-the-scenes dynamic URLs. Since this stage happens to everyone, it's the only place you need to put your rules.

Merganser

6:07 am on Feb 1, 2012 (gmt 0)

10+ Year Member



I see how the single rule might be shorter. In truth, I have a few more conditions than I listed in this post so I'm thinking the $1/$2 stuff keeps it nice and tidy (for my way of thinking anyway).

Ok - I see your point on no need to block static and dynamic versions, and after some reconfiguration and testing, agree. I also decided to concentrate on page 1 initially and make sure it was right before deploying to pages 2 and 3. Thus, I am only working with page 1 for the moment. Here is my code:

# Block bogus queries on page1.php
# This is done statically only (i.e., page1/88)
RewriteCond $1 (ht|f)tp: [NC,OR]
RewriteCond $1 (\?|%3F) [NC,OR]
RewriteCond $1 trackback [NC]
RewriteRule ^page1/([^\ ]+)/?$ - [F]

# Internal rewrite to page1 from new static URLs to dynamic form needed for page functionality
RewriteRule ^page1/([^\ ]+)/?$ /page1.php?item=$1 [L]

# External redirect for page1 if old dynamic URL is used
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page1\.php\?item=([^\ ]+)\ HTTP/
RewriteRule ^page1\.php$ [mywebsite.com...] [R=301,L]

This seems to work fine. What do you think of my use of "([^\ ]+)/?$" in the first 2 rewrite rules? Does this seem OK? In the first rule my intent was basically to capture anything following "page1/" on out the the end of the URL without using .*. I am thinking I have specified any and all characters up to the first space, and all this might or might not be followed by a /. Perhaps I don't need the trailing /? and/or $ anchor?

Thanks for all your help.