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
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):
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
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..
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
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):