301 .htaccess redirect with queries just after / - .htaccess

I've been struggling to do 301 redirects, and none of the existing topics helps.
I need to make redirects with .htaccess for the following pages:
Redirect 301 http://www.mypage.com/?q=company/contacts http://www.mypage.com/contacts
Redirect 301 http://www.mypage.com/?q=product/new/ghz/name-5 http://www.mypage.com/name-5
I know that I should use rewrite rules and specify {QUERY-STRING} and believe me, I've tried. Nothing helps.

Matching against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=company/contacts
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=product/new/ghz/name-5
RewriteRule ^$ http://www.mypage.com/name-5? [R=301,L]
Or the query string (this matches rewritten URIs)
RewriteCond %{QUERY_STRING} ^q=company/contacts$
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{QUERY_STRING} ^q=product/new/ghz/name-5$
RewriteRule ^$ http://www.mypage.com/name-5? [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]

.htacces redirect with special signs

Need help ;)
How to redirect this link?
mydomain.com/selbständige?catid=141&id=141:kredit-für-selbstständige-ohne-schufa
Tested with:
RewriteCond %{QUERY_STRING} ^(.+&)?selbständige?catid=141(&.+)?$
RewriteRule ^/$ http://test.com? [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+&)selbst%E4ndige?catid=141(&.+)?$
RewriteRule ^/$ http://test.com? [R=301,L]
redirect 301 "/selbständige?catid=141&id=141:kredit-für-selbstständige-ohne-schufa" http://test.com
...and so on.
No normal workflow works.
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /selbst.+ndige\?catid=141&id=141:kredit-f.+r-selbstst.+ndige-ohne-schufa [NC]
RewriteRule ^ http://test.com? [L,R]
I used .+ to match against special chars in the uri.

.htaccess - 301 redirect with no query string, leave as is if query string

I put in a 301 redirect for my /index.php as recommended for SEO purposes. Unfortunately this prevents any params being accepted.
example.com/index.php should redirect to example.com
example.com/index.php?anything should stay at example.com/index.php?anything
example.com/index.php?f=bar&b=foo should stay at example.com/index.php?f=bar&b=foo
My attempted fix is below:
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s\/index\.php\s
RewriteCond %{QUERY_STRING} =""
RewriteRule . http://${HTTP_HOST}/ [R=301,L]
Turns out the answer for the scenario is a combination of Deadooshka's answer and a POST fix.
RewriteCond %{THE_REQUEST} \s\/index\.php\s
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

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]

Redirect 301 Not working the way I intended it to!

Trying to get a simple 301 redirect with htaccess using this code:
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
The results are sending me to
http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/?page=cat/radiator-cages/product/radiator-support-cage
Any idea what I'm doing wrong?
Thanks in advance for any help.
--Update--
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
I suggest trying to use the Redirect 301 statement first.
Your htaccess should then look somthing like this
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
Edit:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^/cat/radiator-cages/product/radiator-support-cage/$ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/ [R=301,L]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
http://www.gerronmulder.com/common-seo-rewrite-rules-for-apache-using-htaccess/

Resources