Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect POST htaccess

This question is very similar to: Is it possible to redirect post data? (asked eariler) but that answer does not seem to work for me.

I have a form:

<form action="http://a.test.com/contact" name="contact" method="post">

and inside of a add-on domain, (test.com is an addon), there is a subdomain (a.), and inside of there I have a file item.php, and .htaccess

my htaccess is as follows:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

#normal rewrites
RewriteRule ^[~|-]?([a-zA-Z0-9]+)[/]*$ item.php?user=$1 [NC,L]

note: I left it as [NC,L] because when I changed it to [NC,P] it gives me a 500 server error.

and my item.php

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";

and no matter what the form contains, the $_POST is blank... however, if I do http://a.test.com/item.php?user=contact as the action.

all goes well. POSTing skips the htaccess, and the solution on SO doesn't seem to work.

Thanks in advance

like image 487
willium Avatar asked Jan 09 '11 10:01

willium


People also ask

How do I redirect one link to another link in htaccess?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

What is R 301 L in htaccess?

The R=301 means that the web server returns a 301 moved permanently to the requesting browser or search engine.

How do I redirect a URL to another URL?

Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination.


1 Answers

Your "add trailing slash" rule forces a header redirect:

 [R=301,L]

a header redirect will drop POST values.

You will have to drop that rule, or disable it for POST submissions:

# Forces a trailing slash to be added

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
like image 149
Pekka Avatar answered Oct 10 '22 03:10

Pekka