301 Redirect to new domain with some specific URLs - .htaccess

I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.

But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]

Related

How to redirect from multiple pages?

I need to redirect from 2 pages to 1 using htaccess within one site.
Redirect 301 /product/one/two/blah/ https://test.com/product/one/rabot/vid/
Redirect 301 /product/one/two/woow/ https://test.com/product/one/rabot/vid/
Seems to me I should try RewriteCond something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteRule ^(.*)$ https://www.test.com/[R=301,L]
UPDATE. Tried this service https://www.301-redirect.online/htaccess-rewrite-generator and generated code works:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^product/stroitelnoe\-oborudovanie/prigotovlenie\-betonnykh\-smesey/badi\-dlya\-betona/$ https://psvolga.ru/product/stroitelnoe-oborudovanie/dlya-betonnyh-rabot/prigotovlenie-betonnyh-smesej/? [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^product/stroitelnoe\-oborudovanie/prigotovlenie\-betonnykh\-smesey/betonosmesiteli/$ https://psvolga.ru/product/stroitelnoe-oborudovanie/dlya-betonnyh-rabot/prigotovlenie-betonnyh-smesej/? [R=301,L]

.htaccess redirect new domain and new url structure

I'm setting up redirects from an old domain to new with a different URL structure. A couple of questions concerning the setup below.
First, will this capture both www and non www URLs as well as HTTP and HTTPS? Does the sufficiently redirect one URL from the old domain to the new domain with a URL structure change?
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^page-1/?$ https://www.newdomain.com/page-one [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^page-2/?$ https://www.newdomain.com/page-two [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^/?$ https://www.newdomain.com/ [R=301,L]
Second, I'd like to set up a rule that catches other unspecified redirects to redirect to the homepage. Would this work?
# catch all rule
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule . https://www.newdomain.com/ [R=301,L,NC]
Thanks for the help.

redirect all subdomains with htaccess

I have some unused subdomains and I would like to redirect them to my homepage.
I have found this .htaccess code
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]
It redirects when you call the exact subdomain, for example: test.example.com
But it does not work if you call anything else, for example: test.example.com/meh
It's possibile to redirect any URL in these subdmoains to the homepage?
You may use this rule:
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
%{REQUEST_URI}at the end of target will add previous URI to new target.

Redirect all other pages than the ones specified in my "301 redirect URLs"

I have a list of specific pages I redirect with the "Redirect 301" rule.
Now I'm looking to redirect all the other pages I didn't specificaly mention in this list.
How can I do that?
.htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?parachute\-club\-cannes\.com$ [NC]
RewriteRule .* http://parachute-club-cannes.com%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\.parachute-club-cannes\.com$ [NC]
RewriteRule ^ http://parachute-club-cannes.com%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /www.pccannes.com http://www.parachute-club-cannes.com

301 Redirects with dynamic URLs

I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]

Resources