1

There are domains old.test.ru and test.ru. How do I redirect all pages from old.test.ru/* to test.ru/*?

I have these .htaccess:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   RewriteEngine On
   RewriteBase /

   # redirect from index.(php|asp|aspx|html|htm), main.(php|asp|aspx|html|htm), home.(php|asp|aspx|html|htm)
   RewriteRule ^(.*)index\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)main\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)home\.(php|asp|aspx|html|htm)$ $1 [R=301,L]

   # redirect from dev and www
   RewriteCond %{HTTP_HOST} ^old\.test\.ru$ [OR]
   RewriteCond %{HTTP_HOST} ^www\.test\.ru$
   RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]
</IfModule>

I think that last line

RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]

should work, but it doesn't.

It works for:

old.test.ru/about.php -> test.ru/about.php
old.test.ru/company.php -> test.ru/company.php
old.test.ru/contact.php -> test.ru/contact.php

But didn't work for:

old.test.ru/some/about.php -> test.ru/some/about.php
old.test.ru/some1/company.php -> test.ru/some1/company.php
old.test.ru/some2/contact.php -> test.ru/some2/contact.php
old.test.ru/some2/subsome/contact.php -> test.ru/some2/subsome/contact.php

What should I change?

mjk
  • 2,443
  • 4
  • 33
  • 33
  • possible duplicate of [.htaccess redirect all pages to new domain](http://stackoverflow.com/questions/1945568/htaccess-redirect-all-pages-to-new-domain) – ljs.dev Sep 18 '13 at 15:46

2 Answers2

1

Try replacing your last rule with this:

 # redirect from dev and www
 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^ http://test.ru%{REQUEST_URI} [R=301,L]

Update: To remove index.php etc from URL:

 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^(.*?)((?:index|default)\.(?:php|html?))?$ http://test.ru/$1 [R=301,L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Unfortunately, it doesn't work ... Now even redirect with old.test.ru to test.ru stopped working ... – Valentin Borisenko Sep 18 '13 at 13:41
  • Make sure to put this code before any other rule you have. Also tell me what is your Apache version? – anubhava Sep 18 '13 at 13:50
  • Server version: Apache/2.2.23 (FreeBSD) Server built: Nov 10 2012 20:55:13 – Valentin Borisenko Sep 18 '13 at 13:57
  • ok, place this code just below `RewriteBase` line and test in a different browser. – anubhava Sep 18 '13 at 14:02
  • .htaccess: ` Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^(.*)index\.(php|asp|aspx|html|htm)$ $1 [R=301,L] RewriteRule ^(.*)main\.(php|asp|aspx|html|htm)$ $1 [R=301,L] RewriteRule ^(.*)home\.(php|asp|aspx|html|htm)$ $1 [R=301,L] # redirect from dev and www RewriteCond %{HTTP_HOST} ^dev\.test\.ru$ [OR] RewriteCond %{HTTP_HOST} ^www\.test\.ru$ RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC] ` – Valentin Borisenko Sep 18 '13 at 14:08
  • You still have other rules above my suggested rule. Place my rule just below `RewriteBase` line also tell me what is your URL that used to test this? – anubhava Sep 18 '13 at 14:13
  • If your URL starts with `dev.` then your `RewriteCond` should have `RewriteCond %{HTTP_HOST} ^(www|dev)\.dslaboratories\.ru$ [NC]` Also is there any .htaccess anywhere inside `spectralDNC`? – anubhava Sep 18 '13 at 14:22
  • Of course its `^(www|dev)\.ds...`. Yes there is another .htaccess in `/spectralDNC/`: ` RewriteEngine On RewriteRule ^index\.php$ /spectralDNC/ [R=301,L] ` – Valentin Borisenko Sep 18 '13 at 14:29
  • Ah that .htaccess is the reason of this problem. If you notice `http://dev.dslaboratories.ru/foo` just redirects fine :) – anubhava Sep 18 '13 at 14:31
  • If you have `.htaccess` in `spectralDNC` then you 2 options, either place above code in `spectralDNC/.htaccess` before all other code OR else move that simple rule to `DOCUMENT_ROOT/.htaccess` – anubhava Sep 18 '13 at 14:34
  • Ok, Keep in mind if any subfolder has .htaccess then it takes over. – anubhava Sep 18 '13 at 14:48
0

I would use Redirect on old.test.ru rather than using mod_rewrite if you want to redirect all requests.

In the configuration for old.test.ru use:

Redirect permanent / http://test.ru/
Qben
  • 2,617
  • 2
  • 24
  • 36
  • It doesn't work too - there is an error: On this page, discovered cyclic forwarding. – Valentin Borisenko Sep 18 '13 at 13:44
  • You will have to clean out your `RewriteRule` that do the rewrite from `old.test.ru` to `test.ru` in your `.htaccess` file. `RewriteCond %{HTTP_HOST} ^old\.test\.ru$ [OR]`. – Qben Sep 18 '13 at 14:02
  • don't understand what its mean? – Valentin Borisenko Sep 18 '13 at 14:12
  • I assume you have a `` or similar in the `httpd.conf` for `old.test.ru` it's in this configuration you put the redirect. Also you do not need to do the redirect in `.htaccess` since it's already taken care off. Look in the link I provided and it's explained in there. Is `old.test.ru` only an `Alias` for `test.ru`? – Qben Sep 18 '13 at 14:19