Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect multiple domains to another domain except 1 directory using htaccess?

I am trying to redirect multiple domains to a single domain (which is working fine) but i want one directory to not redirect or change the main domain url.

here is my .htaccess code it works fine until here

this one is working

<pre>
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.com$  [OR]
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.net$  [OR]
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.org$  [OR]
    RewriteCond %{HTTP_HOST}    ^domain.info$ [OR]
    RewriteCond %{HTTP_HOST}    !^www.domain.info
    RewriteRule (.*)    http://www.domain.info/$1   [R=301,L]
</pre>

but when i try to stop redirect of one specific directory by

full code

<pre>
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.com$  [OR]
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.net$  [OR]
    RewriteCond %{HTTP_HOST}    ^http(s)?://(www.)?domain.org$  [OR]
    RewriteCond %{HTTP_HOST}    ^domain.info$        [OR]
    RewriteCond %{HTTP_HOST}    !^www.domain.info   [OR]
    RewriteCond %{REQUEST_URI}  !^/no_redirect_dir/
    RewriteRule (.*)    http://www.domain.info/$1   [R=301,L]
</pre>

it all stops working :( with an error that page is not redirecting correctly.

extra code causing error

<pre>
    RewriteCond %{HTTP_HOST}    !^www.domain.info   [OR]
    RewriteCond %{REQUEST_URI}  !^/no_redirect_dir/
</pre>

any help would be highly appreciated.

thank you!

like image 279
Zain Baloch Avatar asked May 11 '11 00:05

Zain Baloch


People also ask

How to redirect all pages to another domain using htaccess?

How to redirect all pages to another domain using .htaccess: Set the nameservers of the old domain to somewhere you have cPanel hosting; Go into cPanel and add the old domain; Add a .htaccess file that 301 redirects all the internal pages to the new domain; Add a blank index.php for extra safety.

How do I redirect my Old domain to a new domain?

In order to redirect your old domain to a new domain name, you should add the following rewrite rule to your .htaccess file: In order to redirect your site from a non-www to www URL, you should add the following rewrite rule to your .htaccess file:

How to switch a website from one domain to another?

When you need to switch a website from an old domain to a new domain, you need to redirect all your page URLs, this is when htaccess is your friend. The code below will create 301 url redirects for both the www and non-www version of ‘ olddomain.com ‘ to the new domain ‘ newdomain.com ‘.

How to redirect the contents of a directory to another directory?

To redirect the contents of a whole directory to the webserving root: To redirect the contents of a subdirectory to another domain but in the same subdirectory Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them


1 Answers

Write your .htaccess like this:

RewriteCond %{HTTP_HOST} (www\.)?domain\.(org|net|com)$ [NC]
RewriteCond %{REQUEST_URI} !^/*no_redirect_dir/ [NC]
RewriteRule ^(.*)$ http://www.domain.info/$1 [R=301,L]

HTTP_HOST variable just has domain name, no http/https information.

like image 166
anubhava Avatar answered Oct 08 '22 02:10

anubhava