How do I use POST with htaccess rewriterule? - mod-rewrite

I have a rewriterule in my htaccess file that looks like this:
RewriteRule ^Signup$ /dst2/signup.php
I would like to use POST instead of GET for the data that gets passed by this form.
The rule above, however, sends the data as a GET.
Is there a way to use a POST with a rewriterule?

There is no way to reroute a GET action to a POST action with mod_rewrite. If you're sending a POST through a rewrite rule, it should end up being a POST when you're done. If you're sending a GET and trying to change it into a POST, change your system.

Related

Rewrite Very Long URL with Lots of Parameters

I'm trying to rewrite the following long URL in htaccess but keeps redirecting to error page
Existing URL:
https://www.exampledomain.com/stuff-for-sale?location_value=California%2C+USA&property_status=&adm_lvl_1_sn=CA&country_sn=US&location_type=administrative_area_level_1&stateSearch=CA&swlat=32.528832&nelat=42.0095169&swlng=-124.48200300000002&nelng=-114.13121100000001&lat=36.778261&lng=-119.41793239999998&faddress=California%2C+USA&place_id=ChIJPV4oX_65j4ARVW8IJ6IJUYs
I'd like to rewrite simply as:
https://www.exampledomain.com/california-stuff-for-sale
Is this possible? I know that with this approach I would have to do 50 rewrites for each state but the system requires all the parameters to create a proper search.
This is what I tried so far with no luck:
RewriteRule ^/stuff-for-sale?q=&location_value=California%2C+USA&adm_lvl_1_sn=CA&country_sn=US&location_type=administrative_area_level_1&stateSearch=CA&swlat=32.528832&nelat=42.0095169&swlng=-124.48200300000002&nelng=-114.13121100000001&lat=36.778261&lng=-119.41793239999998&faddress=California%2C+USA&place_id=ChIJPV4oX_65j4ARVW8IJ6IJUYs /california-stuff-for-sale[L]

Rewriting url with the following regular expression

I'm trying to do a mod rewrite to get this url: localhost/test/index.php?hello
I created a file for this page called hello.php and it is in the folder /test
To clarify, I have another page that has a link to my hello.php, but what is the correct url so I can display localhost/test/index.php?hello in the url when I click the link to access my hello.php page.
The following doesn't seem like it is right:
RewriteRule ^.*$ index.php?$1 [L]
Try this if you want to just do php files.
RewriteEngine on
RewriteRule (.*)\.php$ /index.php?$1 [L]
To clarify what my answer does. It gives you more friendly URLs which it sounded like what your were asking for.
So you can use localhost/hello.php and it will be internally redirect to /localhost/index.php?hello. Internally means they will never see localhost/index.php?hello and will always see localhost/hello.php in their browser.
You can do any URL and it will rewrite to a php file. e.g. localhost/index.php?newpage and you can use /localhost/newpage.php
Hope that is clearer.
EDIT
You want the reverse but I don't know how your PHP is constructed but query strings are typically field/value pairs. For example name=john, page=contact, action=hello etc. You have index.php?hello but hello has no content or value.
That's probably why you're having such a hard time even re-writing your URL. Using $_GETwould require a value.
So what I would do, is if your URL was like this using field/value pairs
index.php?action=hello
Then in the index.php file you could do something like
$action = $_GET["action"];
if($action == "hello"){
//show contents of hello, include a page or whatever
}
Once you have good URLs it would be easy to rewrite it.
So if the URL that you want shown is like
index.php?action=hello and you want to redirect it to hello.php
Your .htaccess would look like this
RewriteRule ^action=([^/]+) /$1.php [R,L]
That would redirect it to the php file. If you don't want to show the redirection and keep it an internal redirect you can remove the R flag and just keep [L].
I personally don't want the user to see really long query strings URL example. mysite.com?page=music&artist=someartist&title=sometitle
So all my URL's are rewritten to be shorter and friendlier like my original answer.
you don't need .htaccess for 2. as far as you're using GET parametr - use it in index.php:
if (isset($_GET['hello'])) include('hello.php');
this will show the contents of hello.php inside index.php

mod_rewrite handling extra GET variables

I have a mod rewrite rule for pagination of my content:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [L]
However, I am also attempting to handle facebook connect, which will result in a url that looks like this:
http://content.local/page/2?session={"session_key"...
However, with my current rewrite rule, the session variable does not get passed along. How can I pass that variable in addition? I would want to un-rewritten link to look something like /index.php?page=2&session={"session_key"...
Use the QSA flag like this:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [QSA,L]

.htaccess not redirecting to ?_escaped_fragment_=

I want to redirect the request "index.php?_escaped_fragment_=about" to "snapshots/snap_about.html"
but I can't get it to work :(
It's an AJAX site; the URL I receive when I try to request example.com/index.php?_escaped_fragment_=about is:
example.com/index.php?_escaped_fragment_=about#!about
This is my htaccess
redirect index.php?_escaped_fragment_=about http://www.example.com/snapshots/snap_about.html
redirect /testx.html http://example.com/dummytest/index.html
The redirect /testx.html works.
Maybe I am making a big big mistake, but I am new at this!
I'd probably do this using a RewriteRule. Is this a one-off, or is it a general pattern that could be applied to other URLs? e.g. would you also want
index.php?_escaped_fragment_=blah to redirect to snapshots/snap_blah.html?
If not, something like this:
RewriteRule index\.php\?_escaped_fragment_=about snapshots/snap_about.html [NC,R=301]
If yes, something like:
RewriteRule index\.php\?_escaped_fragment_=(.*) snapshots/snap_$1.html [NC,R=301]

Getting the original REQUEST_URI when using mod_rewrite AND mod_proxy

I'm using a custom.conf file for rewrites and codeigniter for some features of the site, mainly the articles.
My original url gets rewritten, so I have http://example.com/article-a101, this uses the custom.conf file to rewrite to codeigniter/article/read/101. I think I must send this as a proxy call using the [P] flag in mod_rewrite to make it rewrite again in codeigniters .htaccess file. Once it hits the code igniter .htaccess, it uses that mod rewrite structure to call the index file and use the article controller and the read function sending in the 101 as the parameter.
What I'm trying to figure it is how do I get the original url in the address bar as its not in the $_SERVER variable. Since I use the [P] on the first rewrite, request_uri has codeigniter/article/read/101.
custom.conf
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2 [P,L]
codeigniters .htaccess, fairly basic
RewriteRule ^(.*)$ index.php?/$1 [L]
Here's my current solution that I know there must be a better method for
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2?orig_url=%{REQUEST_URI}&%{QUERY_STRING} [P,L]
This stays hidden from the user, and I can access the original url through the query string, but doesn't seem like an elegant solution.
I'm pretty sure you cant do it any other way with mod_rewrite
but you could do it with codeigniter routing.
$route['^([_a-zA-Z0-9-]+)-a([0-9]+)$'] = "article/read/$2";
assuming your controller is named article and your function is named read
if you visited /article-a101
then $this->uri->uri_string(); would return article-a101 (the original url, which should be in your url bar now)
and $this->uri->ruri_string(); would return article/read/101 (where you actually are)

Resources