Rewrite Rule to redirect the entire website except one route

I am transferring a website, say example.com to yyy.com .

But example.com/a , and everything like example.com/a/* , should stay on example.com .

But there is something else: I have routes called something like example.com/ab/* , and this should be redirected to yyy.com/ab/* (as well as the rest of the site).

I can get the site to redirect everything except example.com/a* correctly, but that means example.com/ab not redirecting ...

I tried to write the following rules in my .htaccess, in vain:

 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !^a$ RewriteCond %{REQUEST_URI} !^a\/.*$ RewriteRule ^(.*)$ http://www.yyy.com/$1 [R=301,L] 

or even:

 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^ab RewriteRule ^(.*)$ http://www.yyy.com/$1 [R=301,L] RewriteCond %{REQUEST_URI} !^a RewriteRule ^(.*)$ http://www.yyy.com/$1 [R=301,L] 

(Changing the name of the route is not an option)

+4
source share
1 answer

Try the following in your .htaccess file

 RewriteEngine On RewriteBase / #if it starts with routes ab then send to yyy RewriteCond %{REQUEST_URI} ^/(ab/.*)$ [NC] RewriteRule . http://www.yyy.com/%1 [R=301,L] #if it does not start with an a, then also send to new site RewriteCond %{REQUEST_URI} !^/a [NC] RewriteRule (.*) http://www.yyy.com/$1 [R=301,L] 
0
source

Source: https://habr.com/ru/post/1386053/


All Articles