.htacess redirect 301 doesn't work properly - apache

I have issue to redirect 301 URLs,
I want to redirect from fr.example.com/fr to www.example.com/fr-ch
the rest of the URL doesnt have always same structure but htaccess redirect to same structure,
exemple:
RewriteCond %{HTTP_HOST} ^fr\. [NC]
Redirect 301 /fr https://www.example.com/fr-ch
Redirect 301 /fr/coffee/arabica-robusta https://www.example.com/fr-ch/ccc/arabica-robusta
what I get as result is redirect to
https://www.example.com/fr-ch/coffee/arabica-robusta
and not to
https://www.example.com/fr-ch/ccc/arabica-robusta
Am I missing something ??

You need to redirect EXACTLY /fr, so you need to add ^ in the beginning and $ at the end.
So this should work :
Redirect 301 ^/fr$ https://www.example.com/fr-ch

Related

how to redirect url using custom logic in .htaccess?

I want to redirect url using htaccess file from project root folder.
I can easily redirect url like www.xyz.com/ar/contact to www.xyz.com/contact using this
Redirect 301 /ar/contact/ /contact/
but how to redirect url like this
www.xyz.com/ar/shop/xyz.html to www.xyz.com/shop/xyz.html
I have tried this
Redirect 301 /ar/shop/^(.*)$ /shop/$1
Redirect /ar/shop/^(.*)$ /shop/$1 [R=301,L]
Redirect ^/ar/shop/(.*) /shop/$1
but it did't work.
Could you please try following. Written and tested with shown samples only.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ar/(.*)/? [NC]
RewriteRule ^(.*) /%1 [R=301,NE,L]
You can not use a regex based pattern in Redirect directive. If you want to redirect everything that comes after /ar/shop/ to /shop/ then just use a static pattern /ar/shop/ .
Redirect 301 /ar/shop/ /shop/
This will redirect all URIs starting with /ar/shop/ to /shop/ . forexample /ar/shop/foobar will get redirected to /shop/foobar > .

Alternative for bulk Redirect 301 in htaccess

I have some link redirects in htaccess in which every link should be added with /en after .com i.e.,
mydomain.com, mydomain.com/abc.html
to
mydomain.com/en, mydomain.com/en/abc.html
After some research i found that redirect 301 does it. But I think this is not perfect and permanent solution. In future if I add another URL, again I have to add it to htaccess. So can any one help me out in getting the perfect and permanent solution for this.
I have done this in my htaccess
Redirect 301 /about.html /en/about.html
Redirect 301 /contact-us.html /en/contact-us.html
Redirect 301 /index.html /en/index.html
Redirect 301 /locations.html /en/locations.html
You can use this single redirect rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^(?!en/)(.+\.html)$ /en/$1 [L,NC,NE,R=301]
(?!en/) is negative lookahead to assert that URI is not already starting with /en/.

301 redirects - problems with dashes

For some unknown reason some of my 301 redirects work fine and some don't I cannot for the life of me work out why.
These ones are fine:
# Permanent URL redirect
RewriteEngine on
Redirect 301 /uk http://www.mysite.co.uk
Redirect 301 /uk/about-us http://www.mysite.co.uk/about-us/
Redirect 301 /uk/privacy-policy http://www.mysite.co.uk/privacy-policy/
Redirect 301 /uk/withdrawal http://www.mysite.co.uk/withdrawal-consent/
Redirect 301 /uk/promotions http://www.mysite.co.uk/promotions/
These ones do not work:
Redirect 301 /uk/feedback-enquiries http://www.mysite.co.uk/feedback/
Redirect 301 /uk/success-stories http://www.mysite.co.uk/testimonials/
and I get these 2 URLs returned as 404s:
http://www.mysite.co.uk/feedback-enquiries
http://www.mysite.co.uk/success-stories
It's as if there's an issue with the hyphens/dashes..but only when the page name has actually changed and only when the original pages uses a hyphen.
Use below code(Replace each Url with concerning main/redirect url) - -
RewriteCond %{REQUEST_URI} ^/uk/feedback-enquiries$
RewriteRule .* http://www.mysite.co.uk/feedback/ [L,R=301]
All your url should work by this pattern, Let me know if still you face any issue.

Htaccess not working with?

I'm trying to redirect a list of URLs, but any of them that have a ? it isn't working. For example, here's my line of code...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html?print=1&tmpl=component https://example.com/index.php/calendar/
This doesn't redirect. However, if I use this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html https://example.com/index.php/calendar/
...or this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html&tmpl=component https://example.com/index.php/calendar/
...and test the URLs out, they redirect correctly.
Any ideas on how to get this working?
You can't match query string using Redirect directive. Use mod_rewrite instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=1&tmpl=component$ [NC]
RewriteRule ^calendar-of-events/day/date/2013-03-28/227\.html$ https://example.com/index.php/calendar/? [L,R=301]

Simple 301 redirect in .htaccess with query string does not work with Redirect directive

I am trying to redirect a single URL in a .htaccess file with Redirect:
Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/
I have a bunch of other similar rules which work using directory structure URLs, but this one refuses to get processed.
Redirect 301 /old/url/ /new/url/
Do I have to do anything special?
Thanks!
With Redirect you can only test for URL paths, or more specifically, URL path prefixes but not for the URL query. But you can do so with mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} =option=com_rss&feed=RSS2.0&no_html=1
RewriteRule ^index2\.php$ /something/somethingelse/? [L,R=301]