uberweiss Posted October 30, 2008 Share Posted October 30, 2008 i have a small script on http://www.mysite.com/script.php. I would like to redirect all ppl who go this website to https://www.mysite.com/script.php. How do i do this? Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/ Share on other sites More sharing options...
feidakila Posted October 30, 2008 Share Posted October 30, 2008 <?php header('Location: https://www.mysite.com/script.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678337 Share on other sites More sharing options...
uberweiss Posted October 30, 2008 Author Share Posted October 30, 2008 i've pasted the suggested code into the top of the script.php file. but i get the following error when i go to the http://www.mysite.com/script.php Warning: Cannot modify header information - headers already sent by (output started at C:\web\www.mysite.com\script.php:1) anyone know what this means? what i can do? Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678343 Share on other sites More sharing options...
bobbinsbro Posted October 30, 2008 Share Posted October 30, 2008 it means you already sent code to to client. headers are the first thing to be sent, and must be output to the client before anything else. but i'm assuming you're echoing a "you're being redirected" msg or something similar. just move the header() up to before you output anything else. however, i'm not sure anything after that will get displayed... Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678345 Share on other sites More sharing options...
JasonLewis Posted October 30, 2008 Share Posted October 30, 2008 A lot of people say that you should always place headers at the top of the page. This is not entirely true. You should structure your code and display everything at the end of the page, not in little droplets. A lot of people echo on the fly, then encounter header errors so they use output buffering. Output buffering is not a solution, merely a bandage to hide the wound. Properly structured code will eliminate header errors. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678349 Share on other sites More sharing options...
uberweiss Posted October 30, 2008 Author Share Posted October 30, 2008 I did. you can see the code below... <?php header('Location: https://www.mysite.com/script.php'); ?> <form name="upload-form" id="upload-form" method="post" action="./upload.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1024000"> <fieldset> <legend>File upload:</legend> <dl> <dt> <label for="file">File:</label> </dt> <dd> <input tabindex="1" accesskey="b" name="file" type="file" id="file" /> </dd> </dl> <input tabindex="2" accesskey="l" type="submit" name="cmdupload" value="Upload" /> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678351 Share on other sites More sharing options...
predator12341 Posted October 30, 2008 Share Posted October 30, 2008 it needs to be above the <html> tag so it is thevery very first thing Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-678486 Share on other sites More sharing options...
uberweiss Posted October 31, 2008 Author Share Posted October 31, 2008 I've inserted the code before the html tag, but i still get the same error message.. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679116 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Better off letting Apache handle it: RewriteEngine on RewriteCond {%HTTPS} !on Rewriterule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L] Put that in your httpd.conf in a <Location> block or put in in a .htaccess file. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679123 Share on other sites More sharing options...
uberweiss Posted October 31, 2008 Author Share Posted October 31, 2008 I added the suggested code to my httpd.conf file in the <location> block, removed the comment # and rebootet the server. But it didn't work. where should i put the .htaccess file in order to try that. I'm starting to get a bit annoyed at this, to think something that sounds so easy could be so frustrating. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679152 Share on other sites More sharing options...
feidakila Posted October 31, 2008 Share Posted October 31, 2008 try to put only the header statement in a new file, and check if it works, if it does is because you're sending headers in your file or in an included one. I agree with you that the headers issue is confusing. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679183 Share on other sites More sharing options...
uberweiss Posted October 31, 2008 Author Share Posted October 31, 2008 doesnt work if i make a file who looks like this <?php header('Location: https://www.mysite.com/script.php'); ?> <html><head><title>TESTING</title> </head> <body> </body></html> I still get that warning that was mentioned earlier. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679206 Share on other sites More sharing options...
feidakila Posted October 31, 2008 Share Posted October 31, 2008 please, try with a file that only contains this <?php header('Location: https://www.mysite.com/script.php'); ?> is your file included or required in any other "parent" file?? Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679211 Share on other sites More sharing options...
uberweiss Posted October 31, 2008 Author Share Posted October 31, 2008 same warning as before... Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679219 Share on other sites More sharing options...
feidakila Posted October 31, 2008 Share Posted October 31, 2008 ??? no idea man Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679256 Share on other sites More sharing options...
uberweiss Posted October 31, 2008 Author Share Posted October 31, 2008 made the function in javascript instead... it works.. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679259 Share on other sites More sharing options...
waynew Posted October 31, 2008 Share Posted October 31, 2008 Copy and paste the entire file please. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679272 Share on other sites More sharing options...
waynew Posted October 31, 2008 Share Posted October 31, 2008 JavaScript can be turned off by the user. Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679274 Share on other sites More sharing options...
JonnoTheDev Posted October 31, 2008 Share Posted October 31, 2008 Forget the header command or any other type of script redirect (could ruin Google rankings). Use a proper 301 redirect in your .htaccess as DanielO suggested. RewriteEngine on RewriteCond {%HTTPS} !on Rewriterule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/130712-redirect-http-to-https/#findComment-679283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.