Redirect request with POST data using .htaccess
By default, if you want to redirect request with POST data, browser redirects it via GET with 302 redirect. This also drops all the POST data associated with the request. Browser does this as a precaution to prevent any unintentional re-submitting of POST transaction.
But what if you want to redirect anyway POST request with it’s data? In HTTP 1.1, there is a status code for this. Status code 307 indicates that the request should be repeated with the same HTTP method and data. So your POST request will be repeated along with it’s data if you use this status code.
From https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html,
If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
I did this in one of the website to redirect HTTP request to HTTPS for a specific page.
RewriteEngine on | |
RewriteCond %{SERVER_PORT} !443 | |
RewriteCond %{REQUEST_URI} string_to_match_in_url | |
RewriteCond %{REQUEST_METHOD} POST | |
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=307] |
For this to work, you need to have mod_rewrite enabled on the server. The first RewriteCond checks if the request is NOT coming as HTTPS, otherwise it will go in endless loop. If yes, the second RewriteCond checks if the URL contains a string we are looking for. Then if that URL is being submitted as POST method. If everything matches, then we redirect the request using HTTPS secure protocol retaining the POST method and the associated data.
Magento redirect from observer
Redirection in observer doesn’t work normally as it do in Blocks, templates and controllers. Also there is no standard code to redirect from observer that works in every situation.
You will need an argument to achieve redirect when using below code:
public function observingMethod(Varien_Event_Observer $observer) | |
{ | |
$observer->getRequest()->setParam('return_url',$urlToRedirect); | |
} |
Note that $observer object should have getRequest() method to make above code work. You may need to use $observer->getEvent()->getFront()->getRequest() otherwise, or simply var_dump/Mage::log $observer to get better idea what methods the object have.
Or you can use below code which is not recommended: Continue reading »
PHP redirect without header()
It’s very frustrating when you want to redirect from one page to another, and you get the error:
Warning: Cannot modify header information – headers already sent by (output started at /some/filename.php:12) in /some/filename.php on line 99
The problem is Headers are already sent, or in simple language the output is already sent to browser when your header() function is called. The best solution is not to pass headers (send html/output to browser) till the point header() function is fired.
Another way is to use output buffering. Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. For using this method, you have to start your file with ob_start(); so that output gets buffered.
But what if you don’t care whether the output is sent or not, you anyway want the redirect to work? This simple function will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.
function redirect($url) | |
{ | |
if (!headers_sent()) | |
{ | |
header('Location: '.$url); | |
exit; | |
} | |
else | |
{ | |
echo '<script type="text/javascript">'; | |
echo 'window.location.href="'.$url.'";'; | |
echo '</script>'; | |
echo '<noscript>'; | |
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; | |
echo '</noscript>'; | |
exit; | |
} | |
} |
jQuery redirect page, with javascript alternative
jQuery redirect one page to another using the following code:
var your_url = 'http://ka.lpe.sh/'; //change this to your url | |
$(location).attr('href','your_url'); |
Above code works to redirect page in jQuery, but javascript code is good alternative here.
Using javascript to redirect your page using below code, which is better than jQuery, will do the job:
window.location.replace("http://ka.lpe.sh/"); |
Another javascript approach which will mock as if the URL is being clicked to redirect is:
window.location.href = "http://ka.lpe.sh/"; |
Even this will do the trick in javascript, which sets your URL in a window object and assigns it to anchor tag’s href attribute automatically:
window.location = "http://ka.lpe.sh/"; |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)