1

my project has multiple domains and I would like to redirect 100 pages for 1 of these domain.
Now I found this post on how to set up conditions for 1 domain.

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA] 
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC] 
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA] 

My question would be, how can I set this up for 100 pages?
Do I have to duplicate the condition for each link?

What I want to do is this

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^some   https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page   https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings   https://www.mypage.de/shop/settings [R=301, L]

5 Answers5

2

There are so many scenarios to do in this case , for example , you could redirect every page except specific page like this :

 RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC] 
 RewriteCond %{REQUEST_URI} !^/(page1|page2|page3|whatever)
 RewriteRule ^(.*)$   https://www.mypage.de/shop/$1 [R=301, L]

But , if the number of page who should be excluded is large , you could create directory , for example , new then move all files that have to be redirected at it , then make this rule

 RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC] 
 RewriteCond %{REQUEST_URI} !^/new/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI}  -f
 RewriteRule ^(.*)$ /new/$1 [L]

Then you could add this to redirect any URI contains new to that new location :

  RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC] 
  RewriteCond %{REQUEST_URI} ^/new/
  RewriteRule ^(.*)$   https://www.mypage.de/shop/$1 [R=301, L]

So code should look like this :

 RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC] 
 RewriteCond %{REQUEST_URI} !^/new/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI}  -f
 RewriteRule ^(.*)$ /new/$1 [L]
 RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC] 
 RewriteCond %{REQUEST_URI} ^/new/
 RewriteRule ^(.*)$   https://www.mypage.de/shop/$1 [R=301, L]
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
0
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [QSA]

This will redirect every single page.

If you only want 100 specific pages you must add one by one. Like other answers were provided.

Diogo Jesus
  • 318
  • 4
  • 19
  • Thank you. I am not sure if this would work only for one domain or for all. But anyway I need to be able to redirect these 100 specific pages and not all pages. – Chriss Baumfleisch Mar 15 '18 at 10:50
  • Your regex doesn't make sense. `([^/d]+)/` -> every character except the character `d`. – Ron van der Heijden Mar 15 '18 at 10:52
  • @RonvanderHeijden you're right. sorry it's still morning .. answer updated. Remove your -1 please :) – Diogo Jesus Mar 15 '18 at 11:14
  • 1
    Yes, you updated, but still the answer is not what the OP is looking for. This will redirect every page, OP asks for 100 specific pages. – Ron van der Heijden Mar 15 '18 at 11:16
  • OP mentioned in the comments that there were more than 100 pages. Which wasn't included in the main question. Nothing that I could figure out when I first wrote the answer. @RonvanderHeijden – Diogo Jesus Mar 15 '18 at 12:09
0

Probably Skip flag will be the best aproach.

It looks like this:

RewriteCond %{HTTP_HOST} !^www\.site1\.com [NC]
RewriteRule  .?  -  [S=100]
RewriteRule ^some   https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page   https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings   https://www.mypage.de/shop/settings [R=301, L]
.....
RewriteRule ^rule_for_100nd_page   https://www.mypage.de/shop/settings [R=301, L]

How this works.

  1. Test HOST if NOT skip 100 (S=100) the next RewriteRules if YES RewriteRule .? - [S=100] is ignored
  2. Write your 100 rules for www.site1.com source domain and go on.
SlaWitDev
  • 457
  • 2
  • 9
  • and... Is this it ? – SlaWitDev Mar 15 '18 at 17:29
  • Show your .htacces / part of this. May be I can help. – SlaWitDev Mar 16 '18 at 09:35
  • It fine. I posted my solution and it works fine. I was just hoping to avoid the extra line but it doesn't matter. Thx for the help – Chriss Baumfleisch Mar 16 '18 at 09:37
  • Probably you miss `!` (exclamation sing befor domain name) S[kip] in this case works with negation. It means if HOST is NOT in checked domain do S=100 (or other number) if does - S=100 is ignored and process next rules [for domain www.site1.com]- in this way you have only on test per domain name for any you want rewriterules. – SlaWitDev Mar 16 '18 at 09:41
0

Assuming your page names are same after redirect as you've shown in your question, you can use a single rule like this as your top most rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.site1\.com$ [NC] 
RewriteRule .+ https://www.mypage.de/shop/$0 [R=301,NE,L]

Make sure to use a new browser for your testing.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • the answer is not what the OP is looking for. This will redirect every page, OP asks for 100 specific pages. – Diogo Jesus Mar 15 '18 at 12:11
  • This for question title that says **htaccess rewrite based on domain** also do read assumption at the start of my answer. – anubhava Mar 15 '18 at 13:37
0

So after testing some of the suggestions I ended up having to add the condition to each rule in order to make it work.

So as far as I can tell you can't set conditions for more than one rule if the targets are to specific.

So my solution for this issue looks like this.
Thanks for all the suggestions.

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^some   https://www.site1.de/shop/some [R=301, L]

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^page   https://www.site1.de/random/page [R=301, L]

RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC] 
RewriteRule ^settings   https://www.site1.de/whatever/settings [R=301, L]