Remove parameters from redirect - .htaccess

I have set up a redirect in an .htaccess file where I'm trying to remove the parameter from being added to the redirect rule. The rule I have set up is:
RewriteCond %{HTTP_HOST} ^windward\.net$ [NC]
RewriteCond %{QUERY_STRING} ^cat=1$
RewriteRule ^2013/$ https://www.windwardstudios.com/? [R=301,L]
I'm trying to redirect from 2013/?cat=1$ to https://www.windwardstudios.com/ but the redirect URL is:
https://www.windwardstudios.com/?cat=1
How do I remove the ?cat=1 from the redirected URL?

try this
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)&?cat=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [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]

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 RewriteRule change domain .com to .jp

I want to redirect requests on abc.com to abc.jp by adding this line to .htaccess file
RewriteRule ^(.*)abc\.com(.*)$ $1abc.jp$2
But it didn't work. So confusing...
Try using RewriteCond to match the domain, then redirect using RewriteRule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://abc.jp$1 [NC,L,R=301]

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]

rewrite file to subdomain

We used this rules
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.+)$
RewriteRule ^news\.php$ http://news.mydomain.cop/news.php?n=%1 [L,R=301]
for redirect
http://mydomain.com/news.php?n=100
to
http://news.mydomain.com/news.php?n=100
but we need
http://mydomain.com/news.php?n=100
and
http://news.mydomain.com/news.php?n=100
redirect 301 to
http://news.mydomain.com/100.html
Try:
RewriteCond %{HTTP_HOST} ^(news\.)?mydomain\.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.+)$
RewriteRule ^news\.php$ http://news.mydomain.com/%1.html? [L,R=301]
This will make it so if someone types: http://mydomain.com/news.php?n=100 or http://news.mydomain.com/news.php?n=100 in their browser's URL address bar, they'll get redirected to http://news.mydomain.com/100.html, thus changing the URL in the address bar.

Resources