RedirectRule - Am I Missing something?

Topics: User Forum
Dec 17, 2009 at 3:29 AM

RewriteCond %{REMOTE_ADDR}
RewriteRule ^/(.*)$ /$1 [F]

RedirectRule ^/$ /Default.asp [R=301]
RewriteRule ^/Default.asp$ /Page.asp?page=1 [L]
RedirectRule ^/Page.asp?page=1$ /Default.asp [R=301]

As it is written and works, any incoming requests for http://localhost/ are redirected to http://localhost/Default.asp (works great!)

My Default.asp is actually Page.asp?page=1

What is not working are the 301 redirects for requested /Page.asp?page=1 to jump to /Default.asp

Did I miss something?????

Dec 17, 2009 at 3:37 AM

yes, you missed something.

The ? in the line
    RedirectRule ^/Page.asp?page=1$ /Default.asp [R=301]

has a meaning in the regular expression language - the language in which the URL patterns are expressed.  If you want to match an actual ?, then you need to escape it, like so:
    RedirectRule ^/Page.asp\?page=1$ /Default.asp [R=301]

Also, the . (dot) has a meaning too - it's a wildcard. So, the real rule you should use is :
    RedirectRule ^/Page\.asp\?page=1$ /Default.asp [R=301]

These special meanings only apply in the pattern - only on the first expression after RewriteRule or RedirectRule. The special meanings do not apply in the replacement pattern (The thing that comes next) because that string is not a "regular expression".  

Dec 17, 2009 at 4:05 AM
Edited Dec 17, 2009 at 5:47 AM

Thanks Cheeso. It always figures that it would be something simple (read: noob)

Happy Holidays!

I will be making a donation to this project because I am building a CMS system that is focused on ease of use and search engine friendly URLs, and this app works perfect for making that happen.


Dec 18, 2009 at 5:48 AM

Great to hear,.

Glad it's working for you.

Donations are very much appreciated.