Alternative for bulk Redirect 301 in htaccess - apache

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/.

Related

301 redirect (.htaccess) of entire domain to another domain index

I have tried solving this on my own but I can't seem to figure it out.
Here is what I'm trying to do:
Create a 301 redirect of all URLs (including index) on subdomain.example.com to example.com.
So for example:
subdomain.example.com redirects to example.com
subdomain.example.com/blog-post redirects to example.com
This means that I DO NOT want to keep the same URL structure that's used on subdomain.example.com. I just want URLs on the subdomain to point to the index on the example.com. I know that's not the best solution for SEO, but that's how I want it.
I have tried accomplish this using
Redirect 301 / https://example.com/
but that keeps the URL structure and I don't want that.
So how can I solve this?
according to the documentation https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
keeps the part of the url.
With redirectMatch you can use regexes and substitution to rewrite your url
e.g.
RedirectMatch 301 . https://example.com/
where the dot (.) is a match anything.
You can not achieve this with a Redirect as it appends the old URL path to new one. You can use RewriteRule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) https://example.com/ [R=301,L]

.htacess redirect 301 doesn't work properly

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

Apache Conditional RedirectMatch

I have moved a website from /blog/ to the web root. I created a RedirectMatch 301 for my URLs which I believe is correct:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.*)$ /$1
This works correctly. Now, domain.tld/blog/foo/ redirects to domain.tld/foo/
However, there is one issue. There is a page on the website with the slug: /blog/
Now you can't access the page since domain.tld/blog/ redirects to domain.tld/
So what I would like to accomplish is not to redirect /blog/, but to redirect /blog/everything/else/
domain.tld/blog/ no redirection
domain.tld/blog/foo/ redirects to domain.tld/foo/
domain.tld/blog/foo/bar/ redirects to domain.tld/foo/bar/
Etc.
Many thanks for your help!
Can you try this? I think it needs a little change:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.+)$ /$1
Remember to clear cache + history on browsers before testing, browsers remember 301 redirect rules.
Hope that helps.

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.

Website article .htaccess 301 redirect

I have a site in a folder called patients and my urls look like so:
http://site.com/patients/post-name
http://site.com/patients/articles/another-post-name
I want to redirect them all to a clean:
http://newsite.com/post-name
http://newsite.com/another-post-name
In other words, lose the /articles/ which sometimes appears and 301 to the new site.
Any help on how to do this with htaccess?
Edit your .htaccess in http://site.com server and put this(must be the first rule):
RewriteEngine On
RewriteRule .* http://newsite.com/ [R=301]
or
Redirect 301 / http://newsite.com/
Regards.