Forum Moderators: phranque

Message Too Old, No Replies

Ahhh! form method POST stopped working after htaccess change!

added simple ^domain.com to www.domain.com and all forms using Post stopped

         

johnhamman

7:26 pm on May 23, 2007 (gmt 0)

10+ Year Member



Hi all, I have the following and it kills all form Posts.
Querystring still works, but not _Post.
The only 2 lines I added were


RewriteCond %{HTTP_HOST} ^thedomain.com [NC]
RewriteRule ^(.*)$ http://wwwthedomain.com/$1 [L,R=301]

The whole file looks like this.


RewriteCond %{HTTP_HOST} ^thedomain.com [NC]
RewriteRule ^(.*)$ http://www.thedomain.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} (.+)
RewriteRule cat_(.*).html index.php?act=viewCat&catId=$1&%1 [L]
RewriteRule cat_(.*).html index.php?act=viewCat&catId=$1 [L]
RewriteCond %{QUERY_STRING} (.+)
RewriteRule prod_(.*).html index.php?act=viewProd&productId=$1&%1 [L]
RewriteRule prod_(.*).html index.php?act=viewProd&productId=$1 [L]
RewriteCond %{QUERY_STRING} (.+)
RewriteRule info_(.*).html index.php?act=viewDoc&docId=$1&%1 [L]
RewriteRule info_(.*).html index.php?act=viewDoc&docId=$1 [L]
RewriteCond %{QUERY_STRING} (.+)
RewriteRule tell_(.*).html index.php?act=taf&productId=$1&%1 [L]
RewriteRule tell_(.*).html index.php?act=taf&productId=$1 [L]

I want to redirect any non "www" to www.thedomain but I need the rest to work too!
If I remove the www.redirect it works fine.

jdMorgan

8:12 pm on May 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For security reasons, HTTP POSTs should never redirected -- Many browsers won't allow it, and since a redirect requires the browser's cooperation, there is no server-side work-around.

Quick fix: Exclude POSTS from the redirect:


RewriteCond %{REQUEST_METHOD} !^POST$
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

However, the right way to fix this is to correct your forms and scripts to stop them from trying to POST to your non-canonical domain variants -- For this example, they should all post to www.example.com. That way, their POSTs will never need to be redirected by your new code.

[added]
I think you'll find your serve runs faster if you use more-specific patterns, anchor those patterns, and escape "special regular-expressions-token" characters as required. Example:


RewriteRule ^tell_([^.]+)\.html$ index.php?act=taf&productId=$1 [L]

This saves the pattern-matching engine a whole lot of extra work trying to find a 'floating' match inside the input URL.

For more information, see the concise regex tutorial cited in our forum charter [webmasterworld.com].
[/added]

Jim

[edited by: jdMorgan at 8:19 pm (utc) on May 23, 2007]

g1smd

7:52 pm on May 25, 2007 (gmt 0)

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



Your domain canonicalisation code should appear after the specific redirects, and specific redirects should force the domain in the target URL... otherwise you create an unwanted redirection chain.