redirectmatch changes post to get - .htaccess

I have two separated vhost. On the www. one, I have instaled this only line on .htaccess
redirectMatch 301 ^(.*)$ http://d_blur_blur_s.com$1
All works as expected except that in the case where the POST is converted to GET.
Please notice that the post has parametres as a get (i didn't do it, and I won't change it) i am just trying to avoid duplicated content. I am showing firebug trace.
I expect to have the POST redirected on the main domain with redirectmatch or other trick.
Update
I have half of the internal links on the site written with www and the other half without it. So I need to keep both public but not duplicated. I need the GETs forwarded as GETs and the POSTs frowarded as POSTs. the problem is big, I have 12000 pages indexed, and lot of forms. So I am first searching for a generic solution without changing code. I full control de server.
Thanks a lot

A redirect response from your server for whatever request you made, regardless of it being POST or GET request, always results in your client making a GET request (unless you somehow enable it to NOT automatically follow redirects--sometimes I do this with curl, but I don't know any widely used browsers with this functionality available easily to the user). The client browser takes the URL provided by the redirect and treats it as a URL to perform a GET request. No way around it, the POST data is discarded by the server since it was intended for the server resource at the original URL.
If you redirect outside of .htaccess, say in a PHP file to construct redirect response, your only option is to convert the POST parameters into a string of GET parameters and add it to the end of the URL you send back to the client with your redirect response.
I'm fairly confident there is not a way to do automatic POST parameter appending to redirect in the .htaccess file or in the httpd.conf files whether you use the redirect directive or the rewrite directive via the mod_rewrite module.

You redirect using 307 instead of 301 to keep the post data, but some browsers show a dialog to the user if he is sure he want to sent the post data, so not real pretty.
But I'd rather go and fix the problem at the root. The only way you would get a post to the wrong domain is if the html-form refers to the wrong domain (e.g. <form action="www.d_blur_blur_s/public/main/loginGenerator.php" ...). Just fix the html.
Don't worry about duplicated content as search engines never send post-requests, only get. You current solution should do the trick to prevent duplicated content.

The only way to do a redirect and preserve POST data (that I am aware of) is to use mod_rewrite with mod_proxy and then use P flag in RewriteRule.
On your www host enable mod_rewrite, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^ http://d_blur_blur_s.com%{REQUEST_URI} [P]

I bumped into this POST->GET rewrite as well. An API call like this:
curl -X POST https://example.com/api/user/123456789
was entering my API framework with a GET request method.
The cause seems to be related to POST requests with an empty body. To avoid this issue you can set the Content-Length header:
curl -X POST https://example.com/api/user/123456789 -H 'Content-Length: 0'
or pass something in the body:
curl -X POST https://example.com/api/user/123456789 -d ''

In HTTP 1.1, statusCode (307) indicates that the request should be repeated using the same method and post data. Change the redirect type to fix it.

Adding www to the base URL did it for me.

Related

How to redirect a URL in htacess with query string to the same URL but different query string

Because of removing several languages from a multi-language website, I need to 301 redirect pages that end with ?lang=da, ?lang=de, and ?lang=nl to the same URLs but ending in ?lang=en. It sounds like a common scenario, but I haven't found the right code yet to accomplish this or tried some code because the purpose of that code was either to redirect to one new URL or to replace the URL but not the query string, while I need to replace the query string and keep the URL.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule ^ %{REQUEST_URI}?lang=en [QSD,R=301,END]
For this to work the rewriting module obviously needs to be loaded into your http server and it has to be activated for your http host too.
It is a good idea to start out with a 302 redirection and to only change that into a 301 once you are convinced everything is set up as required. That way you prevent ugly caching effects...
You can implement such rules in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (".htaccess"), but that has performance disadvantages and you also need to enable the interpretation of such files first. Please see the documentation of the tool you are using to learn how to do that.

Apache .htaccess redirect to an anchor

I'm trying to do a one-off damage-limitation redirection to an anchor on a page on a website. A wrong URL got published in some publicity material, like this:
https://mydomain.org.uk/A/B
when what I really wanted to publish was
https://mydomain.org.uk/A#B
Having looked at some other answers it seems that any redirect with an anchor needs to be an absolute URL. So I put this in my .htaccess:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,L]
(note, the .php is correct, A.php is the page file). And it just simply doesn't work. The browser simply loads A.php and displays it from the top.
I know that the rule pattern is matching, because if I make the target be a completely nonexistent page I get a 404 as expected.
Unfortunately my web hosting service doesn't let me use the Apache log, so it's hard to trace what's going wrong. Can anyone guide me to how to do the rewrite properly so that I pass the #anchor all the way through to the user's browser?
Thanks in advance!
When the RewriteRule is processed by the server, it basically changes internally which resource to access, without the browser noticing.
The only way to change the URL in the browser is to use the redirect flag. This will make the webserver send a HTTP 302 response with a Location header, which then will result in the browser changing the URL and requesting the new page. This new URL can contain an anchor.
In your case the following rule should work:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,R,L]
Please keep in mind that anchors are a browser feature so they are normally not sent to the server and therefore neither appear in access logs nor can be used in a RewriteRule.

Redirect to url within another url using .htaccess

I have a problem, I need to redirect users to a URL within a url.
For example:
When the user visits the URL: api.example.com/redirect/www.google.com
I want the user to be redirected to: www.google.com
I've been trying for a while now, but no sucess.
You are probably looking for something like that:
Variant for the main http server configuration:
RewriteEngine on
RewriteRule ^/redirect/(.+)$ http://$1 [L,R=301]
Variant for a .htaccess style file:
RewriteEngine on
RewriteRule ^redirect/(.+)$ http://$1 [L,R=301]
It obviously requires the rewrite module being available inside your local http server.
In general you should prefer putting such rules into the main server configuration, if you have access to that. A .htaccess style file should only be used if you have no other alternative. The reason is that such files are notoriously error prone, hard to debug and really slow the http server down.
A more robust approach would be to take the captured part of the url and hand it over to a script on the server side as a parameter. The script can validate the part, whether it is a valid "url" (to keep things simple here...). And maybe make a head request to check of the target exists and is usable. Only then it redirects. Otherwise it returns a meaningful error message.

Will an redirect setup in IIS 6 work for a form POST?

I have a website where a third party performs an HTTP post to a file - let's call it http://mywebsite/third-party-post.cfm (it's a ColdFusion website).
I want to move the code to a new file - let's call it http://mywebsite/third-party-post-new.cfm.
If I change the file attributes in IIS:
Will this pass the form fields for a POST action, or does a redirect like this work only for GET?
You cannot pass post parameters during the redirect. The web server actually makes a call back to the browser to ask it if it can move the page, the browser then resubmits a request to the URL the server is asking to move it to. This is I believe a 301 call to the browser.You never see this and it happens in milliseconds. When the redirect happens it will destroy any post parameters that were submitted in favor of the new request coming from your browser, the server will see it as a brand new request with no post data.
You will need to pass your data in some other way to the page, either through a GET request or a include of the script you wish to run on the old page.
you might be better off actually using a URL rewrite to change how the web server sees a request for that given page.
the apache script would look something like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^third-party-post.cfm /third-party-post-new.cfm [L,QSA]
The above code will take a request for the orignal page and translate it to a request for the new page. If will still maintain the original URL though but will display the new page.
IIS has a URLrewrite function you can utilize or if you are familiar with Htaccess and apache, you can use a Helicon Ape http://www.helicontech.com/ape/ to read htaccess files for your site.
See post https://stackoverflow.com/a/13628908/2482184. Although this question is about apache the same is true for IIS no matter which version you are using. There are many other options available to bypass this issue. One of them is to create the /third-party-post.cfm file and include the /third-party-post-new.cfm. You can even use a new HTML5 technique to change the url without refreshing the page (see http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/ ).

Is it possible to call a Coldfusion cfc method from within .htaccess?

I'm calling a cfm file form htaccess to perform some manipulation on images like so:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
...
RewriteRule \.(jpg|jpeg|gif|png) services/adaptive-images.cfm
</IfModule>
Inside the cfm file I'm resizing the image.
Question:
Would this also be possible by calling a method inside a cfc file? Something like this:
RewriteRule \.(jpg|jpeg|gif|png) services/ai.cfc?method=resize
Is there a way to make this work?
Thanks for any info!
The question you're asking can be distilled down to a couple of components:
Can a CFC method be called via HTTP?
Can a rewrite rule make an HTTP request?
Obviously the answer to the second one is "yes". And - as you seem to already know - the answer to the first one is also "yes". You demonstrate the syntax for this in your RewriteRule.
The thing you need to consider here is that CF just receives requests from the web server to fulfil an HTTP request. CF doesn't know (or care) whether the request can straight through from the web server unadulterated, or whether it was rewritten by the rewrite module first.
So... if you're getting an error, it's something else. What you're trying to do from a web server / rewrite module / CF standpoint is entirely possible.
I suggest you have a look at your logs and see what's causing the 500 error. Indeed if you have robust exception handling enabled, you should be getting the cause of the error displayed on the screen.
What happens if you call the URL you're rewriting to directly via the browser address bar?

Resources