A have a redirect rule in my .htaccess that forwards agent from A.html to B.html using the following pattern:

Redirect 301 /A.html http://mysite.com/B.html

Since the Redirect directive requires to set the target host, is it possible to let this rule prevail only on a specific host? I have both a test and deploy domain, and only want it on the deploy domain. I can set HTTP conditions for Rewrite rules, but how can I for HTTP Redirects?


I came to the solution that it cannot be done with pure Redirect directives. If it is needed to redirect visitor in certain HTTP conditions, must use Rewrite rules.

  • See my response for a way to do it with Mod_Rewrite in your .htaccess file. – Chris S Mar 11 '10 at 19:34

You can actually do this, but you do lose the 301 redirect code.

SetEnvIfNoCase Host "myotherdomain.com" redirectthisdomain

<Files "A.html">
   Order Deny,Allow
   Deny from env=redirectthisdomain

   ErrorDocument 403 http://mysite.com/B.html

just put the redirect rule in the vhost config of your deploy domain. this way it will only be active for this host.

  • I use an external web space therefore I have no permission to the vhost config. That is, I wrote I need to solve my problem in .htaccess. – Török Gábor Jan 20 '10 at 13:29
  • can you use rewrite? the apache rewrite doc has an example which matches your problem: httpd.apache.org/docs/2.0/misc/rewriteguide.html#url – Christian Jan 20 '10 at 13:57
  • Yes, I know Rewrite provides solution for my problem, but I'm curious if it's possible using Redirect. – Török Gábor Jan 21 '10 at 8:29

My scenario is: two domain (domain.org and domain.info) pointing to the same site, and I want that .info is uses instead of .org

The site is written in PHP and at the very beginning of each page there is someting like this

if (stristr($_SERVER['SERVER_NAME'], 'domain.org') === FALSE) {
    // do standard initial operations
} else {
    // insert here the code to rebuild the correct path and put it in $newpath
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.domain.info/$newpath");

In this way you inform both the users and the search engines crawler about your configuration.

  • @Irosa: You're right, but I do not use PHP but static HTML files. I'm looking for a solution with pure Redirect rules in my .htaccess but it seems so they are not capable in this case. – Török Gábor Jan 26 '10 at 9:29

A quick example, if the visitor goes to dev.example.com/A.html then they get redirected to B.html. If they go to www.example.com/A.html then they get redirected to C.html; both with the 301 redirect.

RewriteEngine on
RewriteCond %(HTTP_HOST) ^dev.example.com$ [NC]
RewriteCond %(REQUEST_URI) ^/A.html
RewriteRule ^(.*)$ http://dev.exmaple.com/B.html [R=301]

RewriteCond %(HTTP_HOST) !^dev.example.com$ [NC]
RewriteCond %(REQUEST_URI) ^/A.html
RewriteRule ^(.*)$ http://dev.exmaple.com/C.html [R=301]

You can read more about Mod_Rewrite at Apache's website.

  • thanks I know it. I do know Rewrite rules. I was curious if I can do the same with simply Redirect rules. – Török Gábor May 7 '10 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.