301 redirect with url parameters - .htaccess

To keep google ranking i need to relink for exemple
Redirect 301 /kataloger.asp?id=1 http://www.domain.se/kataloger/category/kopplingar
Redirect 301 /kataloger.asp?id=2 http://www.domain.se/kataloger/category/ventiler
and in others ignore all but the id
Redirect 301 /produkter.asp?id=15 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
Redirect 301 /produkter.asp?id=15&l=3 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
Redirect 301 /produkter.asp?id=15&l=4 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
But they all relink to /kataloger/ and /produkter/ and additionally throw a 404 att google.
What do i do wrong?

Found the answer here: How to 301 redirect old urls with query strings
RewriteCond %{QUERY_STRING} id=1$
RewriteRule (kataloger.*) /kataloger/category/kopplingar? [R=301,L]
RewriteCond %{QUERY_STRING} id=15&(.+)$
RewriteRule (produkter.*) /produkter/kopplingar/a-lok-kopplingar/? [R=301,L]

Related

How to Redirect every link beginning with the same word in .htaccess?

I need to redirect a lot of urls beginning with the same word to a new link.
Exemple:
Redirect 301 /old-text-1/ /new-link/
Redirect 301 /old-text-2/ /new-link/
Redirect 301 /old-text-3/ /new-link/
Redirect 301 /old-text-4/ /new-link/
Redirect 301 /old-text-5/ /new-link/
...
Can I redirect all this links in a better way?
I tried:
RewriteRule ^/old-text(.*)$ /new-link/ [R=301,L]
but it doesn't work.
You need to remove the trailing slash when using RewriteRule directive in .htaccess context.
RewriteRule ^old-text(.*)$ /new-link/ [R=301,L]

Htaccess Redirects with Parameters (need to be removded)

For an migration of a new website i need to redirect /example.html?id=2 redirected to /example/new-page.html
When i create an redirect like this:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html
Redirect 301 /example2.html?bla=34 https://www.url.com/example/new-page2.html
Redirect 301 /eteste.html?yolo=2 https://www.url.com/example/new-page3.html
It returned into this:
https://www.url.com/example/new-page.html?id=2
etc
Change it into this doesn't work either:
Redirect 301 /example.html?id=2 https://www.url.com/example/new-page.html?
Is there something i'm doing wrong (yes! ;))
You need to use mod_rewrite to match against the query string :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^example.html$ https://www.url.com/example/new-page.html? [NC,L,R]

301 redirect to new page keep query string

I want to do a 301 redirect from old_page.php to new_page.php and keep all query strings if they exist.
so old_page.php would redirect to new_page.php
old_page.php?test=1 would redirect to new_page.php?test=1
Here is what I have below, but it's a 404 error.
RewriteEngine on
RewriteRule ^old_page.php(.*) new_page.php$1 [R=301,L]
This worked:
Redirect 301 /old_page.php /new_page.php

How can I redirect URL's with numbers using by htaccess?

I need redircet a lot of pages on my site.
all those page url is:
/index.php?act=download&id=9&mirror=0
/index.php?act=download&id=9&mirror=1
/index.php?act=download&id=9&mirror=2
/index.php?act=download&id=10&mirror=0
/index.php?act=download&id=10&mirror=1
Etc.
I need to write in my htaccess file this code:
Redirect 301 /index.php?act=download&id=9&mirror=0 http://mydomain.com
Redirect 301 /index.php?act=download&id=9&mirror=1 http://mydomain.com
Redirect 301 /index.php?act=download&id=9&mirror=2 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=0 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=1 http://mydomain.com
Redirect 301 /index.php?act=download&id=10&mirror=2 http://mydomain.com
but I have a lot more..
how can I redirect all of this:
/index.phpact=download&id=ANY-NUMBER&mirror=ANY-NUMBER
to my main page? (http://mydomain.com)
You cant match query string using Redirect directive. Use mod_rewrite instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^act=download&id=[0-9]+&mirror=[0-9]+
RewriteRule ^index\.php$ http://mydomain.com? [L,R=301,NC]

yet another htaccess redirect request - old domain to new domain and old pages to new pages

Spent a day reading through hundreds of variations all over the web but still can't find a definitive answer that works for this.
Moving old site structure on old domain to new site structure on new domain so I am looking for the best, search engine friendly, redirect code that:
permanently redirects all old-domain.com and www.old-domain.com traffic to www.new-domain.com.
Adds specific redirects from old-domain.com/old-page.html and www.old-domain.com/old-page.html to www.new-domain.com/new-page/
Here's what I have - and it isn't working - can someone improve/fix this for me please. Many thanks!
RewriteEngine on
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]
redirect 301 /old-index.html http://www.new-domain.com/
redirect 301 /old-page-1.html http://www.new-domain.com/new-1/
redirect 301 /old-page-2.html http://www.new-domain.com/new-2/
redirect 301 /old-page-3.html http://www.new-domain.com/new-3/
You should stick with using only mod_rewrite or mod_alias and not mix the two. And order matters
mod_alias:
Redirect 301 /old-index.html http://www.new-domain.com/
Redirect 301 /old-page-1.html http://www.new-domain.com/new-1/
Redirect 301 /old-page-2.html http://www.new-domain.com/new-2/
Redirect 301 /old-page-3.html http://www.new-domain.com/new-3/
Redirect 301 / http://www.new-domain.com/
mod_rewrite:
RewriteEngine on
RewriteRule ^old-index.html$ http://www.new-domain.com/ [R=301,L]
RewriteRule ^old-page-1.html$ http://www.new-domain.com/new-1/ [R=301,L]
RewriteRule ^old-page-2.html$ http://www.new-domain.com/new-2/ [R=301,L]
RewriteRule ^old-page-3.html$ http://www.new-domain.com/new-3/ [R=301,L]
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]

Resources