Above code must solve your problem
- Make sure the file name is
.htaccess (mind the dot)
- Make sure .htaccess file is in your public_html folder.
- Make sure your code is correct.
Once you output any kind of string with echo or pront functions apache server sends the response to the client with http headers. So, all the response headers are sent when you echo the first string. You can’t use header function after this process. You can use above code to use output buffering technique which means the output is not sent until full script is executed. Some other fixes are… add header function to the top of your php script, don’t add any empty rule before <?php tag.
If you call header after html code, the best fix is mentioned above by @teodor