M
M
mrSaizer2021-05-07 01:14:05
htaccess
mrSaizer, 2021-05-07 01:14:05

Redirect domain and subdomain via htaccess with one rule?

Good afternoon!
My htaccess on domain1 looks like this:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !^/file.php
  RewriteRule ^(.*) https://domain2.com/ [L,R=301]
</IfModule>


Redirect all pages to domain2, except for one file.
The problem is that the redirect works only for the main domain, for a subdomain (for example, sub.domain1) my htaccess rule no longer works.

In general, here is the question - how can I correct the rules in such a way that absolutely all pages of the domain and domain1 subdomain redirect to domain2, except for one file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zettabyte, 2021-05-07
@Zettabyte

If you are using .htaccess, then you most likely do not have access to the configuration of the web server (Apache or its replacement).
Also, most likely, your subdomain and domain have different "home folders" - for example, public_html\domain1.comand public_html\subdomain.domain1.comif you use cPanel. Accordingly, when accessing a subdomain, files are taken from its home directory, and one .htaccess is not enough - they are taken from different folders.
I see two options:

  1. Set both the domain and subdomain as the home (root) folder to the same folder. Then they will be served by the same .htaccess. But all other files will also be the same.
  2. Put .htaccess one level above the domain and subdomain home folders. For the example above, this would be public_html. However, this option needs to be tested separately. On different panels (and maybe on different servers), this works differently. Also, for the shared .htaccess to work, both root folders must not have their own .htaccess. Otherwise, the latter will apply.

The htaccess for point 1 would be something like this (your file.php exception needs to be saved):
RewriteCond %{HTTP_HOST} ^(.*\.)?domain1\.ru$
RewriteRule ^.*$ https://domain2.ru/$0 [L,R=301]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question