1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to redirect all pages other than index.php to other website ?

Discussion in 'Apache' started by poseidon, May 13, 2007.

  1. #1
    I want to direct all pages of my site, other than index.php to say example.com/test.php than how can I do it using .htaccess :)

    ANy help will be highly appreciated
     
    poseidon, May 13, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use mod_rewrite if enabled. Place the following in a .htaccess file in the root of your domain (domain.com/.htaccess):
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{SCRIPT_FILENAME} !index\.php$
    RewriteRule ^(.*)$ http://www.anotherdomain.com/page.php [L,R]
    Code (markup):
     
    rodney88, May 13, 2007 IP
  3. poseidon

    poseidon Banned

    Messages:
    4,356
    Likes Received:
    246
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Rodney this is the second time you are helping me. ... (few months back too u had helped me)

    Can u tell me how did u become so good in htaccess ? I just don't understand all the L and R and such stuff :(
     
    poseidon, May 13, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's much simpler than it looks - you just need a bit of trial and error to get familiar with it and then you can do pretty much anything.

    If you already know regex from programming languages you're halfway there in terms of using mod_rewrite (the most powerful and commonly used module for URL manipuation). The actual syntax is fairly straightforward. For instance, the first few lines will always be the same:

    A security feature of the module requires that the FollowSymLinks option be enabled so we do this with the line
    Options +FollowSymLinks.

    Then we tell Apache we want to use rewrite engine by turning it off. Pretty self-explanitory
    RewriteEngine On.

    You may not always need the next line but when placed in .htaccess files, you can define the rewrite base - that is, which directory we want to work in. To start in the root of your domain, you specify
    RewriteBase /

    Then you have the really powerful stuff, your rewrite rules. A rule is just like any other command you give a server. It must be in the format

    RewriteRule PATTERN DESTINATION FLAGS

    where pattern is the regex pattern to apply the rule to. For example, in the code I posted we used ^(.*)$ as the pattern. Without going into too much detail, ^ indicates the start of the pattern, . matches against any character and * means we're repeating the previous character (i.e. any character) 0 or more times. We group it in parenthesis so we can use it in the destination as a backreference. Finally we have the $ to denote the end of the string. This pattern will therefore match any and every requested file.

    DESTINATION is where we want to rewrite to. If you're doing a redirect, this can be a full URL. If you just want to do a rewrite behind the scenes, you use the path. We want to go to another website so we've just put in that URL there.

    FLAGS can be left out if you want but these allow you to modify the behaviour of the rule. The most common ones are R (redirect), L (last - stops the processing of any more rewrite rules) and NC (no case makes the match case insensitive). A flag must be in square brackets and if you use more than one flag, separate with a comma.

    But that's not all - we only wanted to redirect if we weren't requesting index.php so we add another line, the RewriteCond. This is a condition which allows you add flexibility to rewrites. If you have a RewriteCond line, the line after it will only be processed if the specified condition is met. Here we simply check the server variable %{SCRIPT_FILENAME} which contains the name of the requested file (surprisingly enough) against a pattern that ends in index.php. And we use the ! to negate the pattern so that we only apply the rewriterule if the requested filename is not index.php.

    Hopefully that makes some more sense to you..
     
    rodney88, May 13, 2007 IP
  5. poseidon

    poseidon Banned

    Messages:
    4,356
    Likes Received:
    246
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Rodney, unfortunatley I just realized that on my site, non index.php urls are in this format index.php?id=12345 and I want to redirect all such urls. The code that u have given is not working :)
     
    poseidon, May 15, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So you want to redirect if there's a query string and not if there's not. I.e. redirect all index.php?ANYTHING but not index.php alone?

    If so, you could try:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^(.*)$ http://www.anotherdomain.com/page.php [L,R]
    Code (markup):
     
    rodney88, May 15, 2007 IP