.htaccess redirect / rewrite condition with exact match - .htaccess

I need to redirect a url like this:
https://zzz.com/Class/Today-Class-100
to
https://zzz.com/otherfolder/
BUT leave a url like this
https://zzz.com/de/Class/Today-Class-100
alone. Right after the .com we have language directories, and those should not redirect. Only the first one should - how do I redirect an EXACT URL and leave ones with any difference alone?
This did not work:
RedirectMatch 301 ^/Class/Today-Class-101$ https://zzz.com/otherfolder/
as it still redirected with a subfolder in front of
/Class/Today-Class-101$

Related

Htaccess 301 redirect is only redirecting domain not also the page

I want to redirect page www.domain1.nl/page-old to www.domain2.nl/page-new.
When www.domain1.nl/page-old is visited it redirects to: www.domain2.nl/page-old.
So the redirect sort of works, but only half. The domain is redirected, the page is not.
Domain1 only has a .htaccess file in the root.
Domain2 is a WordPress website.
We use apache 2.4 + php7.3 + CloudFlare (no rules set)
We tried multiple htaccess rules texts: Redirect 301, Redirect, RedirectMatch, RewriteRule. All same result.
In the .htaccess file of domain1.com:
Redirect 301 /page-old/ https://www.domain2.com/page-new/
Expected result would be:
www.domain1.nl/page-old >> www.domain2.nl/page-new
Actual result:
www.domain1.nl/page-old >> www.domain2.nl/page-old
Posting my answer in comment section here so that readers can find it easily later
You need to use this rule instead for precise matching:
RedirectMatch 301 ^/page-old/ https://www.domain2.com/page-new/
Redirect is non-regex directive that works with starts-with match.

301 redirection not redirecting to the right URL without a trailing slash

I have to manually redirect few URLs in my website1 to website2.
Below is my code in the .htaccess file of website1
Redirect 301 /post1/ https://www.website2.com/post1
When I enter https://www.website1.com/post1/ in the browser it's being redirected to https://www.website2.com/post1 successfully, as expected.
But, When I enter https://www.website1.com/post1 in the browser it's being redirected to https://www.website2.compost1, the slash is missing after https://www.website2.com
What could be done to solve this?
You can do this in a single rule using RedirectMatch that uses a regex to make trailing slash optional as this:
RedirectMatch 301 ^/(post1)/?$ https://www.website2.com/$1
Added benefit is avoiding repeat of post1 in source and target by using a capture group in source and using back-reference $1 in target.
Remove the trailing / from the Redirect.
Redirect 301 /post1 https://www.website2.com/post1
This redirect then works for both versions of the URL. See testing link here.
Using 2 Redirect URLs in this particular order solved it.
Redirect 301 /post1 https://www.website2.com/post1
Redirect 301 /post1/ https://www.website2.com/post1

htaccess Redirect 301 : mysite.com/sub/ to mysite.com/sub/home

my homepage url is like this http://mysite.com/sub/
I just want it to redirect to new url something like this http://mysite.com/sub/home?lang=en
here's my code
Redirect 301 /sub/ /sub/home?lang=en
Problem/error:
the new url becomes like this http://mysite.com/sub/home?lang=enhome
there's unnecessary home concatinated after en
how can I removed this? Or is there something wrong with my code?
don't know there's might be already same question like this
This is because the Redirect directive "connects" 2 path nodes, and you've got one inside the other (/sub/home is inside /sub). For example, if the directive looks like this:
Redirect 301 /a /b
This means when someone requests http://mysite.com/a/foo/bar they get redirected to http://mysite.com/b/foo/bar. What happens when you get redirected to /sub/home is that you get redirected again because /sub/home matches the pattern /sub, and the home gets appended, thus /sub/home?lang=enhome.
You can try using RedirectMatch instead, which doesn't "connect" path nodes:
RedirectMatch 301 ^/sub/?$ /sub/home?lang=en
Or mod_rewrite:
RewriteEngine On
RewriteRule ^/?sub/?$ /sub/home?lang=en [L,R=301,QSA]

Massive .htaccess Redirect

I got some problem on redirecting something.
I tried to use Redirect 301 /link/link/link to /link/link
Is there a way to make it more easier coz there are 100+ links I need to redirect.
Like this
/blog/category/energy-savings/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
To the Homepage?
Thanks
The directive:
Redirect 301 /link/link/link /link/link
Doesn't do what you think it does. It's mapping to nodes together, meaning a request for:
/link/link/link/foo/bar.html
gets redirected to:
/link/link/foo/bar.html
So maybe you need to fix that. You could try:
RedirectMatch 301 ^/link/link/link/?$ /link/link
so the nodes aren't connected like in the Redirect directive. As for trying to "fix" this problem of yours, you can try:
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/\4/ /
This redirects anything that looks like: /blah1/blah2/blah3/same/same/same/ etc. to the homepage at /. The \4 matches the 4th path in the URI, so if anything after the 3rd path repeats, then it redirects.

Redirect only one page Page without affecting other urls with the nam in path

i want to redirect a page:
http://www.mysite.com/samplepage/
Pages like http://www.mysite.com/samplepage/samplepage2/ shall not be redirected.
This is my current .htaccess code:
RedirectMatch 301 ^/samplepage/ /new-samplepageurl
The problem:
This one also redirects url's like:
http://www.mysite.com/samplepage/samplepage2/
Can I limit the redirect only to that one exact path?
Try using this:
RedirectMatch 301 ^/samplepage/?$ /new-samplepageurl
The $ sign marks the end of string, and ? mark makes the trailing slash opional (e.g. both /samplepage and /samplepage/ will be redirected)

Resources