How to make 301 redirect from one website to another where the ending is different? - .htaccess

I have 2 websites:
OLD one - https://www.old.example/en/
NEW one - https://new.example/en
Lastly, Google Search Console reported around 80 improperly redirected links for OLD website, i.e.:
https://www.old.example/en/?p=41310
https://www.old.example/en/?p=45659
https://www.old.example/en/?p=72785
In .htaccess of OLD page is inputted only code:
Redirect 301 / https://new.example/
which redirects above links from OLD page to i.e.
https://new.example/en/?p=62692
How can I correct it and i.e. expect to have in such cases always redirection to main page - https://new.example/en

To remove the query string completely (without a stray ? at the end) you'll need to use mod_rewrite instead.
For example, in the .htaccess at the old domain:
RewriteEngine On
RewriteRule ^(en)/ https://new.example/$1 [QSD,R=301,L]
Aside: Although this many-to-one redirect will likely be seen as a soft-404 by Google.

Related

Remove subfolder using htaccess

I know that this has been requested a million times but i haven't found an answer.
Basically I want to remove amp from my site but I've a million pages indexed using /amp so if I remove it this will cause a mess.
What I want to do is to redirect all the url like these
https://*/amp
will go to the same url without amp.
For example an article with this url
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail/amp
will be redirected to the same article without /amp like
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail
This has to be valid for whatever article url that end in /amp that has to be the /amp removed
Thanks!
Assuming you need to remove amp on requests to the document (first example) and /amp on requests to other URLs then you would need a rule like the following using mod_rewrite, near the top of the .htaccess file:
RewriteEngine On
RewriteRule ^(?:(.+)/)?amp$ /$1 [R=301,L]
The regex ^(?:(.+)/)?amp$ matches both amp and <something>/amp and removes amp or /amp respectively.
Test first with a 302 (temporary) redirect to avoid potential caching issues.

How to redirect a "bad" url that has a query string but does not start with a ? symbol?

There's a site that has had a bunch of bad links indexed and I've been asked to deal with it. There's one type of link that is giving me a headache:
http://www.example.com/category-display.html&Category_Code=some_cat_code
I tried redirecting to the home page:
Redirect 301 /category-display.html& /
That doesn't work because it adds everything past the & to the url.
In the best of worlds, I'd like to redirect to:
/app/mm.mvc?Category_Code=some_cate_code
So I tried using querystring and RewriteRule/RewriteCond but there's no query string without the ? that I can figure out, so I'm kind of stuck here.
Any ideas?
You can use this rule as your top rule in site root .htaccess:
RewriteEngine On
RewriteRule ^category-display\.html&(.*)$ /app/mm.mvc?$1 [L,NC,NE,R=301]

301 Redirect Issues in .htaccess file

I've been digging through the archives and threads and can't seem to find anything that matches my 301 Redirect issue -
I am trying to redirect old links to a new site and a problem with the following:
This one works - Redirect 301 /food-service/ http://www.xxxx.com/food-services.html
This one does not - Redirect 301 /food-service/distribution http://www.xxxx.com/distribution.html
The one that does not work tries to redirect to - http://www.xxxx.com/food-services.htmldistribution/
Would you mind lending me your thoughts on what I can do?
Thank you all!
The documentation for Redirect says:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
The behaviour that you do observe is as we would expect from the documentation. It goes through the Redirect directives, and chooses the first one that matches.
To get the correct behaviour, you have to list the most specific redirect first, and the least specific last.
If you would have rules for /a/b/c, /a/b and /a, then you list them in that order.

How to “redirect” from main page in Prestashop to specific product page?

This question is the same one that was asked on Feb 24.
I applied the answer using my site product page which is:
Redirect 301 / http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
The error message I receive is WEBPAGE NOT AVAILABLE
The URL line contains:
http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.htmlhome/8-the-lifter-scrollsaw-arm-lift-assembly.html (and all of this text- except for the web address - repeats many many times on the URL line.
Try using RedirectMatch instead because Redirect links two different path nodes together. Meaning, / gets symbolically linked to http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
. Thus anything after the / also appears at the end of http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html.
Incidentally, that means you're browser will continue to loop and constantly get redirected by the server. So try:
RedirectMatch 301 ^/$ http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
You need to create a .htaccess file in the /home subdirectory with the following line:
RewriteEngine On

htaccess redirecting from rewritten dynamic urls to new dynamic urls

i am experiencing a very unique problem and i hope someone can help!
so we have recently created a new ecommerce website and we made it live and everything was working great but when we to implement our 301's from our old pages we were getting some wierd things
so the code below actually works
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
this code does not
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
the output when i try to do this redirection is "Invalid parameters specified!" on a blank webpage and in the address bar it has this
http://mysite.com/index.php?cat=1/sub_directory/
we were thinking that maybe the problem is because our old pages were dynamic but mod_rewrite was used to create more readable urls and we have also deleted all our old files because they were interfering with our new pages rendering
any help would be greatly appreciated!
thanks
That is strange, as redirect should only match the specific url listed, where as it looks like its behaving like rewriterule and partially matching the subdirectory url against the first rule..
try putting the more specific rule above the less specific, like so:
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
That way the more specific rule will be hit first, and the /directory/ only rule will only match if more specific matches above fail
alternatively, you could try RewriteRules:
RewriteRule ^directory/$ http://mysite.com/index.php?cat=1 [R=301,NC,L]
RewriteRule ^directory/sub_directory/$ http://mysite.com/index.php?cat=2 [R=301,NC,L]
the ^ and $ anchors should prevent any unwanted partial matching

Resources