How to redirect from multiple pages? - .htaccess

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]

Related

301 Redirect to new domain with some specific URLs

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]

.htaccess 301 Redirect Except Main Page for .html

I'm trying to redirect all URLs in www.oldexample.com/index.html to www.newexample.com/index.html except the homepage (www.oldexample.com/index.html).
I have tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.oldexample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [R=301,L]
but it didn't work. It is redirecting all pages.
How can I make it work?
Please try with below,
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule ^(.*)$ http://www.newexample.com/$1 [R=301,L]

Htaccess - 301 redirect with analytics campaign parameters

I have multiple domains directed to folder with .htaccess
my htaccess looks like this:
RewriteEngine On
rewritecond %{http_host} ^domain-2.com
rewriteRule ^(.*) https://domain-1.com/?utm_source=redirects&utm_medium=domain-2.com&utm_campaign=301 [R=301,L]
rewritecond %{http_host} ^domain-3.com
rewriteRule ^(.*) https://domain-1.com/?utm_source=redirects&utm_medium=domain-3.com&utm_campaign=301 [R=301,L]
And I want do this more effective and elegantly - all domains are redirected to domain-1.com
Exist any way how use {HTTP_HOST} or something similar at URL parameters?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !domain-1\.com$ [NC]
RewriteRule ^(.*)$ https://domain-1.com/?utm_source=redirects&utm_medium=%{HTTP_HOST}&utm_campaign=301 [R=301,L]

Redirecting a web folder directory to another htaccess

Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
Both of attempts are not correct as first will redirect:
http://olddomain.com/foobar to http://newdomain.com/foobar
not to the homepage of newdomain. Same is the problem with 2nd rule.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]

redirect subdomain request to main domain using htaccess

here is what I do and works good:
#redirect subdomains to controller
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)$ blog.php?blog_uid=%1 [L,QSA]
now I want to redirect
http://example.domain.com/post-123.html
to
blog.php?blog_id=example&post_id=123
but this not works:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)/post-([0-9]+)\.html$ blog.php?blog_uid=%1&post_id=%2 [L,QSA]
how do it? actually how redirect such subdomain request to a rewrite condition (it is not a real page).
Use this rule instead:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^post-([0-9]+)\.html$ /blog.php?blog_id=%1&post_id=$1 [NC,L,QSA]
Try this one.
RewriteEngine On
RewriteRule ^post-([^/]*)\.html$ /blog.php?blog_id=example&post_id=$1 [L]

Resources