htaccess 301 redirects to wrong url - apache

So I did a 301 redirect with htaccess but dont understand why the final link is wrong?
This is what I end up with http://example.com/about?/about-us/
Rather than a clean intended http://example.com/about
This is my htaccess code
Redirect 301 /about-us/ /about
To me this looks fine... What have I done wrong?
I need to redirect from http://www.example.com/about-us/
I've also tried
Redirect 301 /about-us/ http://example.com/about

You can try using apache mod_rewrite instead of using Redirect:
RewriteEngine on
RewriteBase /
RewriteRule ^about-us/?$ /about [L,R=301]

Related

.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

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

htaccess Redirect excluding pagination

this is related to: Apache Conditional RedirectMatch
I am redirecting /blog to the root (/)
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.+)$ /$1
This works correctly:
/blog/ - no redirection
/blog/foo/ - redirects to domain.tld/foo/
/blog/foo/bar/ - redirects to domain.tld/foo/bar/
Etc.
I would like to make one modification. My pagination urls are domain.tld/blog/page/1, domain.tld/blog/page/2, etc and they should not redirect.
How can I prevent /blog/page/ from redirecting?
Thank you!
Try :
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/((?!page/[0-9]).+)$ /$1
This will not redirect
/blog/page/id

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.

htaccess redirect subdirectory to root

What rule can I add .htaccess that would give me the following result?
I want to redirect to the homepage any search that includes a certain directory. For example, when anyone tries...
test.com/example
...or...
test.com/example/something
...they end up at...
test.com
Most everything I have tried kinda worked but always ends up appending whatever was after the targeted directory to the domain after the redirect, for example...
test.com/example/something
...leads to...
test.com/something
This is not what I want. Thanks
Code I have tried...
RewriteRule ^example/(.*)$ http://www.test.com [L,R=301]
and
Redirect 301 /example http://www.test.com
UPDATE
This seems to fix it...
RedirectMatch 301 ^/example/ /
Redirect 301 /example http://www.test.com/
Thanks!
Try:
RewriteEngine On
RewriteRule ^example/ / [L,R=301]
or
RedirectMatch 301 ^/example/ /