Forum Moderators: phranque

Message Too Old, No Replies

How to do a mod rewrite that will still allow me to send $ POST data

         

eZACKe

1:19 am on Jul 10, 2012 (gmt 0)

10+ Year Member



I know this must be possible somehow. You can't honestly tell me that everyone who has ever used url redirection has to either submit all their forms via $_GET or use JavaScript.

So how can this be done?

Here's what I have:

RewriteEngine On
Options FollowSymLinks

# check if url ends in a slash and if it does redirect to same url without one
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*([^/.]+))/\ HTTP/
RewriteRule ^(([^/]+/)*([^/.]+))/$ http://localhost/example/$1 [R=301,L]

# extionless url rewrite for urls in form example.com/cmd
RewriteRule ^([^/.]+)$ index.php?cmd=$1 [L,QSA]

# extionless url rewrite for urls in form example.com/cmd/action
RewriteRule ^([^/]+)/([^/.]+)$ index.php?cmd=$1&action=$2 [L,QSA]

# extionless url rewrite for urls in form example.com/cmd/ajax
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)$ index.php?cmd=$1&action=$2&ajax=$3 [L,QSA]


Now this is all well and good until I try to submit a form with $_POST. In that case I get a 303 Move Permanently and the $_POST data doesn't make it. I understand this and understand why for the most part. What I don't understand is how to get around this. How can I do a mod_rewrite that captures the same functionality as above, but will allow me to successfully submit a form with $_POST data?

Thanks for the help! This really has me stumped.

lucy24

7:49 am on Jul 10, 2012 (gmt 0)

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



Have you tried simply omitting POST from the list of methods in {THE_REQUEST}? In practice you shouldn't be seeing anything but GET and HEAD.

If it were not so close to 1 AM I would ask what those QSAs are doing there. Aren't your rewrites in the wrong order?

eZACKe

12:11 pm on Jul 10, 2012 (gmt 0)

10+ Year Member



Well I'm not 100% sure I follow the first part of your response. No matter what though, I need all requests to be directed to the form index.php?cmd=x&action=y&ajax=z (with the action and ajax parts being optional).

To help clarify things so that we can possibly answer mine better, let me tell you what this is doing (as far as I understand).

THe first RewriteRule is taking every request and putting it in the form localhost/example/something/somethineElse/.... without ever having a / following.

The next RewriteRule is working off what we just did in the previous RewriteRule and rewriting it to index.php?cmd=x. This rule will only happen if the request was of the form example/x. I append query strings with QSA because if the url was something like example/x?pg=3 I want to keep the pg=3 part.

The next 2 RewriteRules are basically the same thing, but they work when the url was example/x/y and example/x/y/z, respectively.

I hope my explanation helped a bit. For more information on how this came about, take a look at this thread (which you were even a part of lucy):
[webmasterworld.com...]

So with that information, can my question be answered? Is there a way I can just change my RewriteRules to allow for what I want to do?

Thanks!

lucy24

5:07 pm on Jul 10, 2012 (gmt 0)

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



Heh. I read as far as the second sentence of g1's answer
requests for robots.txt, images, stylesheets, and javascript files are also rewritten to be handled by your index file.

That was kinda my point here.

Your forms originate within your own site, right? So their URLs are already in the correct format, and no further redirecting should be needed. You only ever need to redirect things that arrive from outside with unwanted URLs. You can't possibly be handling GET requests and POST requests in exactly the same way, or everyone would go around in circles.

About the second piece of the Rewrite: Even if you are scrupulous about using [^/]+ so you can't have something with multiple directories (or pseudo-directories) trying to sneak in where there's only room for one, it still seems safer to go from most complicated to least complicated. Start with /a/b/c/ Now anything left over can't be more than /a/b/ And then anything you still haven't picked up has to be /a/ alone.

At every stage, a page request either has a query string when it comes in, or it gains one when it goes out-- depending on whether it's a Redirect or a Rewrite. QSA implies that something is getting redirected or rewritten twice. Or, ahem, that the site isn't using pretty URLs for public view ;)

g1smd

9:52 pm on Jul 10, 2012 (gmt 0)

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



When the patterns don't have an end anchor the rule with the longest pattern must be first.

When the patterns are end anchored the rule order isn't so important. In this case I would order them so that the most frequently used rule is first.

This mod_rewrite stuff has lots of little foibles. :)