My site is online, but I used this code to redirect it to a coming soon page.
<?php
if(!isset($_GET['en'])) {
header("Location: /comingsoon");
exit;
}
I then added third party SSL to my site and when I removed that bit of code the initial site would come up, but after the log in page the sessions are not being stored.
When I introduce that bit of code (earlier) back in it works.
This has really got me baffled.
I used this bit of code in HTAccess to redirect from HTTP to HTTPs.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Not sure if this has anything to do with it.
function redirectToHTTPS()
{
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
}
// call the above function in that page where you have to redirect to https.
Related
I am trying to make clean URLs at my website which i am working on local.
I am using WampServer.
Here is my .htaccess content
RewriteEngine on
RewriteRule ^search/([0-9a-zA-Z]+) search.php?semt=$1 [NC,L]
Here is what i am doing at search.php file.
<?php
if(!isset($_GET["semt"]) || empty($_GET["semt"])){
header("Location: index.php");}
else{
$region = $_GET["semt"];
include('database/config.php');
include('includes/header.php');}
?>
When i am typing,
localhost/project/search.php?semt=xxx
it is quite working.
When i am typing,
localhost/project/search/xxx
it is not working. Browser giving this error.
error screen capture
This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
Final version of the link is that.
localhost/project/search/index.php
How can i solve this problem?
You should see why this is happening from your final url. Change redirect to /index.php.
I'm making a page 404 with button for user report that site have a problem. The idea is when user click on button, I receive a email with information of the last page accessed...
The problem is that I'm using the php variable '$_SERVER['HTTP_REFERER']' and this ever return a null value. I found this question (In what cases will HTTP_REFERER be empty) and I came to the conclusion that this variable is not the solution.
This is my .htacess
RewriteEngine on
ErrorDocument 404 http://example.com/erro.php
#ErrorDocument 404 /erro.php #this doesnt work... redirect doesnt work
On page erro.php I have code with function email that are working without problem, but I need some manner to take last accessed page that generate error.
On page erro.php I'm trying use:
echo $_SERVER['HTTP_REFERER']; // return null value
echo $_SERVER['REQUEST_URI']; // return page http://example.com/erro.php
I try use alternative with jQuery (https://stackoverflow.com/a/2415645/2761794):
On page erro.php
$(document).ready(function() {
var referrer = document.referrer;
alert(referrer); // return null value
});
Some suggestion to take last accessed URL on page erro.php for send by email?
As you are willing to work with mod_rewrite anyway and have PHP there is a slightly different approach.
First you could detect for a request that is for a file or directory that is not there and pass that to the PHP script with a rewrite.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /erro.php?error_path=$1 [R=301,L]
Depending on your exact setup you may need to tweak that last line perhaps to
Rewriterule ^(.*) http://example.com/erro.php?error_path=$1 [R=301,L]
I find that mod_rewrite is somewhat akin to voodoo and can sometimes need a little "try it out and see what works".
Then in your erro.php file:
<?php
$badfile = $_GET['error_path']; // the URL that 404'd
http_response_code(404); // send the 404 header code
// ... your other code
The end result for the visitor should be almost the same but you would have access to the data you need.
Much of the rewrite directive came from this question: htaccess errordocument 404 and pass url to path
For more on setting response codes in php: http://php.net/manual/en/function.http-response-code.php
mod_rewrite cheat sheet: http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/
The official doc on mod_rewrite: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
I'm new at Code Igniter. My index page is named abc_welcome where i've below code
public function index()
{
$this->load->library('session');
$login_data = $this->session->userdata('user');
if($login_data) {
redirect('/product/product_details');
}
else {
$this->display();
}
}
So when a already logged in user hit he can redirect to product page. But strangely it is not redirecting.
Where as at login page i've same piece of code and it is redirecting all right.
So when a user come to my www.abc.com page it's not redirecting (if loged in) and session has no data checked but when cliking login button he is and obviously session got the right data. i want him to redirect first time.... more strangely in my demo site (my laptop) Index redirecting is working!!! but not at hostgator.com.
Thanks in advance.
Farness
Thank you all for your support.
i finally made it work by .htaccess redirecting. i just put below code to my .htaccess and it's working as expected.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^(.*)$ http://abc.com/$1 [L,R=301]
yes #farness put $config['base_url'] = 'http://www.abc.com/'; in your config.php file
From what you're describing, it seems like your code is fine, if it runs locally on your laptop, so it's likely that the session data isn't being stored/read properly. Aside from the base_url above, you should also verify the following settings in /appication/config.php:
$config['cookie_domain'] should be set to .abc.com (with the leading dot) if your base url is www.abc.com
$config['cookie_path'] just to be safe, set this to "/"
$config['cookie_secure'] set to FALSE (unless your site is accessed only using https, keep this as false
Hopefully that gets you up and running.
Everything was working fine till I added my .htaccess file. What I'm trying to do is route all my users to their profile page. So www.darudude.com/user1 routes to www.darudude.com/userinfo.php?user=user1
My .htaccess file as this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]
However ever since I added this, it breaks my sessions. On every page I have a initialize a session, and the sessions stores the referrer. This is the piece of code that handles the important part.
if(isset($_SESSION['url'])){
$this->referrer = $_SESSION['url'];
}else{
$this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
and then I'm returned to the original page using this piece of code:
header("Location: ".$session->referrer);
So for example, without the .htaccess file, if I login through one of the pages everything works and I get redirected back to the page I logged in from (i.e. if I logged in from index.php I get redirected back to index.php; if faq.php, I get redirected back to faq.php). With the .htaccess file I keep getting sent to /userinfo.php leading me to thing its something wrong with my rewriterule
This is how its supposed to work:
index.php loads. the $_SESSION['url'] is set to index.php
a login form is enacted whos action redirects to process.php
process.php the $session->referrer is set from $_SESSION['url']
After the login is confirmed the page should redirect using: header("Location: ".$session->referrer);
This is how it worked originally without any problems.
However, after the .htaccess was created it seems to redirect me to userinfo.php. I think it has something to do with my rule.
Any ideas?
I'm not sure if I understand the problem, but the rewrite rule you're using seems to turn the request to /index.php into a request to /userinfo.php?user=/index.php which may not be what you want.
It's because you're relying on $_SERVER['PHP_SELF'], which does not include the query string (the ?user= part of the URI). It worked before because your pages didn't rely on query strings to uniquely identify themselves. You can use $_SERVER['REQUEST_URI'] instead, though look out for circumstances where you don't want the query string to be preserved and now it is being.
Incidentally, the \?* in your RewriteRule regex is doing exactly the same as thing as if it weren't there.
You could try logging in with AJAX, thus never having to refresh the page at all. A simple Google search will throw up plenty of results, see below. Even if you're not using jQuery (which alot of the tutorials seem to expect you to), it's still possible with basic Javascript, in fact that's how I wrote my AJAX log-in script before converting it to use jQuery later.
http://www.google.com/search?q=php+ajax+log-in
In one of the page I have is where administrators are allowed, however, I use if the session isn't set, the header will redirect them to index.php and that method works.
If I replace index.php with home which is for the htaccess which changes it to index.php but it gives an error in the browser
This works:
if(!isset($_SESSION['MEMBER'])){ header("Location: index.php"); }
This does not work:
if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }
htaccess:
RewriteRule ^home$ index.php
The error in Firefox:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this
address in a way that will never
complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
What's wrong with it? How do I get this method to work?
You're redirecting index.php to home in your php file, and home to index.php in .htaccess. Firefox is telling you there is an infinite redirect loop.
Edit:
index.php and home are the same thing, so redirecting to either will result in an infinite loop. You need to do something like this:
#.htaccess
RewriteRule ^home$ index.php # public page
RewriteRule ^members$ members.php # member only page
And then in index.php
# index.php
if(isset($_SESSION['MEMBER'])){
header("Location: members");
exit;
}
Au Contraire. Here is how you carry PHP sessions id's within an .htaccess redirect...
RewriteEngine On
RewriteRule ^(.*)$ /home/ [QSA,L,E=REDIRECT_URL:$1]
Pay attention to the last bit... [QSA,L,E=REDIRECT_URL:$1]
You've created an infinite loop.
When PHP executes the following code:
if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }
It redirects the browser to /home.
But in .htaccess you rewrite the URL to /index.php. Which then again makes PHP to execute the above PHP code.