October 1, 2012

PHP .htacces and index.php redirection

Question by Emann Tumala Saligue

I am new about this redirection and I want to know how it works?
I have some file called index.php and i want to hadlle it any directory in the site



The url is:

test 1. www.example.com/ - will view the include home.
test 2. www.example.com/settings - will view the include  settings.
test 3. www.example.com/errorsample - will view the include   404 page.

How to make .htaccess and index.php using that code or any idea how it works and sample code of it.

Answer by Starx

I will try to explain this with a simple example. Consider the following .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /index.php?x=$1 [L] 
#  ^ This is the rule that does the redirection

What it does is, it routes every url request and sends the request to index.php. How it send it? I will show couple of example to do that.

  • www.example.com/settings will be send as www.example.com/index.php?x=settings
  • www.example.com/errorsample will be send as www.example.com/index.php?x=errorsample

So, now you are configure your index.php and decided what you want to do, with the value you get in $_GET['x']

switch($_GET['x']) {
   case "profile": include("include/profile.php"); break;
   // .... similarly other cases
   default: include("includes/home.php"); break;


May 3, 2012

error in 301 permanent redirect

Question by asitha

i am using htaccess for 301 permanent redirect.
i have to make a redirect for bima.php to ge.php.so i wrote the below code in htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]

this works properly..whenever i put www.test.com/bima.php in url it will redirect to www.test.com/ge.php
the problem is i have to do a 301 redirect for ge.php also.that means whenever www.test.com/gen.php in url it will redirect to www.test.com/bima.php.
www.test.com/bima.php needs to redirect to www.test.com/gen.php and vice versa.
Any idea?or anyway to do this?

Answer by Starx

Your redirect rule

RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]
RewriteRule ^ge.php?$ $1/bima.php [NC,R=301,L]

Is redirecting in infinite loop. Remove one of them.

No matter what type of logic you use, the redirection you are attempting will end of in loop at one time. So better avoid the need of such requirement.

Here is a PHP Solution

File: ge.php

if($_SESSION['redirected']['from'] != 'bima.php') {
   header("location: bima.php");
   $_SESSION['redirected']['from'] = 'ge.php';

File: bima.php

if($_SESSION['redirected']['from'] != 'ge.php') {
   header("location: ge.php");
   $_SESSION['redirected']['from'] = 'ge.php';
April 23, 2012

PHP redirect on exact date

Question by user990175

I’m using this code to redirect based on the hour of the day, and the day of the week.

Here it is ..


$hour = date('G');
$minute = date('i');
$day = date('w');
$m = $hour * 60 + $minute; // Minutes since midnight.

if (
  $day == 0    // Sunday...
  && $m >= 615 // ... after 10:15…
  && $m <= 700 // ... but before 11:40…
) {
  header("Location: open.php");
if (
  $day == 3     // Wednesday...
  && $m >= 1125 // ... after 18:45…
  && $m <= 1235 // ... but before 20:35…
) {
  header("Location: open.php");


I was wondering if there was a way to redirect to a page based on an exact date in the future like April 25th or November 1st.

Thanks .

Answer by Dan Lee

This is a simple approach, which does not take account the year date:

// 25th april:
if (date('d') == '25' && date('m') == '4') {
  header('Location: open.php');
// 1st nov:
if (date('d') == '1' && date('m') == '11') {
  header('Location: open.php');

Look at the date() documentation for more exact details.

Answer by Starx

Convert the redirection date into timestamp using strtotime() or date() and you can do it easily

if(strtotime("2012/5/3") <= time()) {
     header("location: toredirect.php");
March 8, 2012

PHP Redirect Within website

Question by baburao113

I’ve been trying this code since 3 days in a row until a certain time (minutes or seconds) but unable to solve the problem.

My target is to redirect visitor to 10 random URLs which are being selected from a text file. The user will see a certain page for a certain time and then redirect to another page again, the number of pages he will be redirected to is complete RANDOM.


The problem is the visitor is not being redirected to any other page which is randomly selected from a text file, instead it is just refreshing the page… But I want to redirect him to other pages from the text file.. Hope you guys understood me by now.

EDIT: Found the problem. Actually the $rand_link is having NULL as it’s value.. { [0]=> NULL } Don’t know why…. ANy solution? Checked the ‘BBnormalLinks.txt’ file for it’s permissions and that file is having some links in it for sure because I just checked it..


Here is the CODE:

<?php // Generate Random Nubmers.. 2 ********
            $numbers2 = range(13,70);


            for ($j=0;$j<1;$j++)
                $seconds = numbers2[0];

            //////// For Random URL of Site
            $links = file('BBnormalLinks.txt');
        $rand_link = $links[ mt_rand(0, count($links) - 1) ];                   

                header("refresh:". $seconds .";url=". $rand_link); ?>

Answer by Starx

The syntax is correct but some pointers that can cause this are

  1. Some text have been outputted before the header is passed.
  2. The random page, is not being generated, thus ending up refreshing the same page again and again.

I have a very strong feeling, that your $rand_link is returning blank or null.


After a few discussion, the problem was the evil path again.

$links = file('patotofileBBnormalLinks.txt');

As baburao113, quoted

I had to move that file to wordpress theme folder lol! Problem resolved 🙂

November 29, 2011

Redirection using PHP footer in Worpress

Question by The Waves

I have a question about timed redirects in PHP – specifically in wordpress.

We have setup a site using a free Woothemes placeholder theme, it is very limited. But that is OK – the site is simple.

After 20 seconds I would like the page to redirect to another URL – would it be possible to insert some code into footer.php to do this? I have found what looks like the right code:

// Redirect with a delay:
header('Refresh: 20; url=http://www.example.org/');

Can this be inserted antwhere in footer.php?

Any input is welcome.

Answer by Starx

Yes, it can be inserted, but in case some HTML is already by the server you will receive an warning such as header already sent by ..... and the redirection will not function as it should.

Instead you can perform such by clearing everything in the output buffer using ob_clear() that is to be printed and then send the redirection header.


if($casespecial==true) {
    ob_clean(); //make sure nothing is outputed to the browser
    header('Refresh: 20; url=http://www.example.org/'); //now send the header param

    //After wards, you can resume your normal code and output the template as you require
September 26, 2011

Redirecting a page using Javascript, like PHP's Header->Location

Question by Keith Donegan

I have some code like so:

$('.entry a:first').click(function()
    <?php header("Location:" . "http://www.google.com"); ?>

I would like to know how I can achieve this using Javascript.

Answer by Starx

You application of js and php in totally invalid.

You have to understand a fact that JS runs on clientside, once the page loads it does not care, whether the page was a php page or jsp or asp. It executes of DOM and is related to it only.

However you can do something like this

var newLocation = "<?php echo $newlocation; ?>";
window.location = newLocation;

You see, by the time the script is loaded, the above code renders into different form, something like this

var newLocation = "your/redirecting/page.php";
window.location = newLocation;

Like above, there are many possibilities of php and js fusions and one you are doing is not one of them.

May 13, 2011

How to use jquery rewrite redirect url

Question by user610983

I created a drop down list(with javascript onchange features), I would like to rewrite the redirect url from this: http://localhost/en/abc/kk.php?CT=1 to http://localhost/abc/kk.php?lang=en&CT=1 by using jquery.

Possible to do it?

Answer by Starx

You cannot rewrite the redirect url through clientside script such as javascript itself. You need .htaccess file to do so.

However, if the urls http://localhost/en/abc/kk.php?CT=1 is already present in your markup like in anchor tags

<a href="http://localhost/en/abc/kk.php?CT=1">Some Link Text</a>

Then you can use jQuery to change the value

$(document).ready(function() {
    //It is better to replace the values using pattern mathcing
June 15, 2010

Monitoring for postbacks with PHP?

Question by Rebecca

I have a PHP page with content that my users can view. When this page receives a POST request from a certain external URL, I’d like to redirect the user to another page.

The problems I’m’ having are:

How can I monitor the page for requests being sent in an efficient way?

How can I actually redirect them since header() doesn’t work.


Answer by Starx

we can put this snippet to check the post request

if($_POST['flag']==1) {

But If you want to check the request regularly without user interaction than you will have to use AJAX


Please fill the form - I will response as fast as I can!