Hello,

I'm new to Redirections in Apache web server,

Can anyone tell me how to do the following:

I want to redirect:
http://www.domain.com/index.php?product=(variable)
To:
http://sub.domain.com/index.php?product=(variable)

Variable is numeric, I only want a 301 permanent redirection from domain to subdomains just for that specific category.

Hope someone will help

Well the most basic way in PHP would be to simply use the header function to report the 301 and then notify the user of the new location. That can be done like so:

<?php
//301 Redirect
header('HTTP/1.1 301 Moved Permanently'); 
header('Location: http://sub.domain.com/index.php?product='.urlencode($_GET['product'])); 
?>

Though, if you want every query in the form of index.php?product= to be redirected to the subdomain, htaccess is your best choice. If you have it enabled, use it over PHP if at all possible since the rules in it are executed before PHP parses and runs any scripts. What you are looking for can be done with:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index.php?product=(.*)$ http://sub.domain.com/index.php?product=$1 [L,R=301]
</IfModule>

EDIT: DaniWeb bug above? I set the code type in the above tag to htaccess. Are unknown types being defaulted to PHP or is there some kind of language detector (if so, .ht files look nothing like PHP source)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.