form post to mod_rewrite url - mod-rewrite

I am trying to submit a form to the url "localhost/login". Inside my login directory I have an index.php file with the code I am using to debug that the post is working:
<?php
echo $_POST['username'];
?>
I have a .htaccess file inside my login directory:
RewriteEngine on
RewriteRule ^. index.php [L]
The problem is, when I post to localhost/login my firebug shows that the initial POST goes through, but then redirects to login.php as a GET request without any POST variables...
POST
http://localhost/login?password=test_password&remember=true&username=test_username
301 Moved Permanently
GET
http://localhost/login/
200 OK
Any tips would be great.
Thanks

I have a condition in my .htaccess file:
RewriteBase /
RewriteCond %{HTTP_HOST} !^www(.*)
RewriteRule ^(.*) http://www.%{HTTP_HOST}%{REQUEST_URI}
which rewrites any links without the prefix "www". Like this:
http://mysite.com to http://**www**.mysite.com
And that was the problem I had:
in my form I have forgotten to put the "www" and so my POST array was empty.
Putting the www in the form like this:
action="http://www.mysite.com/login"
instead of:
action="http://mysite.com/login"
fixed the problem for me.

Based on my research, POST should be allowed to be rewritten and come out as POST, any sort of problem is probably due to something else going wrong, like your code.
Btw, in general, to keep the GET parameters from being stripped, use the QSA directive:
[QSA,L]

Related

Mod Rewrite Rule redirects

I was trying to password protect a folder on our server. However, each time I included .htaccess to that folder, and .htpasswd, it was automatically redirecting everyone to a 401 page without any login prompt.
When i visited my root directly, i saw this .htaccess:
RewriteBase /
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^www\.(.+)
#RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
#RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
I commented it out and my .htpasswd & .htaccess to the foldr works, however, as my mod rewrite isn't the best, I wasn't sure if commenting the above will create problems for my website?
thanks.
This is basically just redirecting any www.host.ext to host.ext, and returns a 301 Moved Permanently response code.
If you don't care about that functionality you should be able to comment it out.
post your subdirectories htaccess file and we can see if we can sort your original issue out.

Despite adding QSA to mod_rewrite, $_GET still returning empty variable

UPDATED 2/1/13: This question is really holding me up from getting some work done, so if anyone else has a suggestion, It'd be much appreciated.
I'm trying to make domain.com/notalone/viewpost/5/ act as domain.com/notalone/viewpost.php?id=5
My current .htaccess is this:
RewriteEngine On
RewriteRule ^notalone/viewpost/([0-9]+)/?$ /viewpost.php?id=$1 [QSA]
Currently, the page loads properly when you visit /notalone/viewpost/5/ but it does not pass $_GET['id']
Things I've Tried:
Adding Options -Multiviews ---> Results in a 404
Using Rewritebase / ---> No effect
RewriteRule ^viewpost/([0-9]+)/?$ /viewpost.php?id=$1 [QSA] ---> No effect
You say viewpost.php is in the notalone folder, but you htaccess assumes it is in the root.
So I think it should be:
Options -MultiViews
RewriteRule ^notalone/viewpost/([0-9]+)/?$ /notalone/viewpost.php?id=$1 [QSA]
(Note that I added the multiviews, because that will be your next issue. The fact that the old url worked was because of multiviews.)

mode rewrite redirecting directory to another wont work

alright so i have a need for mode rewrite and i am completely noob/new to it.
I've read sever guide. that explained mode rewrite goes - pattern - redirection - flags.
i am trying to redirect anyone that gets into domain.com/product/ to domain.com/product.php
and pass the variable url=232 however i am first trying to redirect /product/ to product.php
Here is what i got so far.
Options +FollowSymLinks
RewriteEngine On
RewriteRule \/product\/ /product.php
I've started even more basic then that by redirect product.php to index.php and that worked.
am i doing something wrong ?
RewriteEngine On
RewriteBase /
RewriteRule ^product/?$ /product.php?url=232

mod_rewrite for pretty url with uriencoded query strings

ok, so I've tried many things I've found on SO and elsewhere, but I just can't get it to work, always receiving a 404 error code.
I'd like to enter this url:
memorizeit.com/pics/220.0.8251.20120905002352.7982368227/Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter.+Getting+grass+stains.
and have it invisibly convert to:
memorizeit.com/pics/index.php?pic=220.0.8251.20120905002352.7982368227&title=Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter
The index.php page is looking for:
$pic = ($_GET["pic"]);
$title = ($_GET["title"]);
In my .htaccess file in the /pics directory I've got the following:
RewriteEngine on
Header set Cache-Control "max-age=2592000"
RewriteRule ^pics/([^\/]*)/([^\/]*)/([^\/]*)$ /pics/index.php?pic=$1&title=$2&extra=$3 [L,QSA]
RewriteRule ^pics/(.+)/(.+)$ /pics/index.php?pic=$1&title=$2 [L,QSA]
RewriteRule ^pics/([^\/]*)$ /pics/index.php?pic=$1 [L,QSA]
I've tried it without the QSA, I've tried it with either .+ (anything) and [^/]* (anything except /) I left both in so you can see how I've put them there (I think!). I do plan on making the $1 allowed to only include numbers and periods, but I'd just like to get it to work being wide open first.
I can't figure out why it isn't working. In my base url .htaccess file I have:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
And it always redirects to https so I know the .htaccess files are being read. I don't know what else to try. Any thoughts would be greatly appreciated. Thanks!
Turns out that because pics/ is a real directory the rewrite was trying harder to get there than to just go ahead and rewrite it. So I changed the pics/ to p/ in the matching statement and moved the rules to the base .htaccess file and it works as expected.
Wow that took a long painful time... Hope it saves someone else some time.

Redirect an URL with all variables to another URL

I need to redirect a URL to another URL. So far I have done this in the .htaccess:
Redirect 301 http://domain.com/mypage.php http://newdomain.com/mynewpage
This works fine, but it copies the GET variables in the new URL as well. So I end up with
http://domain.com/mypage.php?item=32
being redirected to:
http://newdomain.com/mynewpage?item=32
I want remove all the variables in the new ULR. I know that this has to be done somehow with mod_rewrite, but I am struggling a lot with it and can not make it work.
Any help is appreciated.
Thanks
Ok,I got it myself... here is what I have done:
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) %{REQUEST_URI}?

Resources