RedirectMatch: redirects to wrong URL - regex

I switched a Shop-CMS and put a lot of old URL into the .htaccess to redirect old products to their new location.
But some redirects are wrong:
RedirectMatch 301 ^/products/catxy/313? https://www.example.com/products/catxy/product-1/
RedirectMatch 301 ^/products/catxy/314? https://www.example.com/products/catxy/product-2/
RedirectMatch 301 ^/products/catxy/319? https://www.example.com/products/catxy/product-3/
When I go to example.com/products/catxy/319 I get redirected to product-1 and not to product-3
As I understand the regex above it means starting with /products/catxy/319[MAYBEMORE] -> redirect to product-3
I can't write ^/products/catxy/319$ because there are a lot of different endings for 319 (all variations of that product id).
I don't know either if it would be better to use mod_rewrite in my situation.

Problem is presence of ? in the end of pattern: ^/products/catxy/313?, which is making last digit optional so your first rule matches anything that starts with:
/products/catxy/313
or
products/catxy/31
You probably means to keep trailing slash optional and have your rules like this:
RedirectMatch 301 ^/products/catxy/313(?:/.*)?$ https://www.example.com/products/catxy/product-1/
RedirectMatch 301 ^/products/catxy/314(?:/.*)?$ https://www.example.com/products/catxy/product-2/
RedirectMatch 301 ^/products/catxy/319(?:/.*)?$ https://www.example.com/products/catxy/product-3/
Remember to clear your browser cache before testing the changes.

Related

Redirect of URL with parameter not working as expected

I am wondering which kind of redirect-rule might bring
http://www.gesetzesweb.de/StrRehaG.html+target=
to redirect to
https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/
http://www.gesetzesweb.de/StrRehaG.html is already correctly redirecting to https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/,
but http://www.gesetzesweb.de/StrRehaG.html+target= is falling back to the default https://www.anwalt.org/gesetze/
Hereby you can see which redirect rules are set in place in the .htaccess
RewriteEngine On
RedirectMatch 301 ^/StrRehaG.html?$ https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/
RedirectMatch 301 ^/StrRehaG.html%20target=?$ https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/
RedirectMatch 301 / https://www.anwalt.org/gesetze/
Does anybody have any clue?
I tried to put
RedirectMatch 301 ^/StrRehaG.html%20target=?$ https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/
after
RedirectMatch 301 / https://www.anwalt.org/gesetze/
without success
Thank you very much in advance for any valuable hint.
RedirectMatch 301 ^/StrRehaG.html%20target=?$ https://www.anwalt.org/strafrechtliches-rehabilitierungsgesetz-strrehag/
A + in the URL-path part of the URL is a literal +, not an encoded space. (+ is only an encoded space when used in the query string part of the URL, but you still wouldn't be matching against %20.) So the regex should be like the following instead:
^/StrRehaG\.html\+target=$
Note that the literal + needs to be backslash-escaped in the regex to negate its special meaning as a regex quantifier. Likewise, the literal . should also be backslash-escaped. The trailing ? would seem to be redundant, based on your examples. You can combine the first two rules with the following regex:
^/StrRehaG\.html(\+target=)?$
This makes the trailing +target= part optional. So it matches both /StrRehaG\.html and /StrRehaG\.html+target=.
RewriteEngine On
Note that RedirectMatch is a mod_alias directive. RewriteEngine (mod_rewrite) is therefore not required.

htaccess redirect 301 without a separator

End of the url have numbers without any separator
Given url like
example.com/productdetails/product-flowers123
needed to redirect
example.com/productdetails/product-flowers--123
can i do this with .htaccess
I already tried: RedirectMatch 301 ^/productdetails/([A-Za-z]+)$([0-9]+)$ /productdetails/$1--$2
But its not worked.
You can use this rule instead:
RedirectMatch 301 ^/(productdetails/.*?[a-zA-Z])([0-9]+)/?$ /$1-$2
Make sure to use a new browser or clear your browser cache before testing this change.

htaccess redirect if URI matches and doesn't have anything after it

I'm having a bit of an issue with a 301 redirect to fix some client site requirements.
Here's the scenario:
If I go to website.com/case-studies I would like it to redirect to website.com/case-studies/any/any/any, however, if we have something like website.com/case-studies/technology/any/any it shouldn't redirect.
I have tried Redirect 301 /case-studies /case-studies/any/any/any but this basically put me in a loop of /any * 30 appearing in the address bar.
You will need to a regex based directive and use anchors. So use RedirectMatch instead of Redirect:
RedirectMatch 301 ^/case-studies/?$ /case-studies/any/any/any
Regex ^/case-studies/?$ will match /case-studies or /case-studies/ but it won't match anything beyond that.
Make sure to clear your browser cache when testing this change.

Rewrite URL on .htaccess

I have urls like www.example.com/de/something and I need to redirect to www.example.com everything that starts with /de/.
At the moment I have done this
redirect 301 /de http://example.com
and it redirect all links but just removing /de part and result is www.example.com/something.
How to fix this?
Thanks
redirect directive matchs rest of the uri and appends it to the target, you can use RedirectMatch to redirect a specific uri
redirectMatch 301 ^/de/? http://example.com
If you want /de/ in the target you should have specified so, becasue Redirect will include in the target everything "after" what you have matched.
For a different virtualhost as the destination you want this:
Redirect 301 /de/ http://example.com/de/
or
Redirect 301 /de http://example.com/de
If what you want is redirect inside the same virtualhost /de to /, then use a negative lookahead.
RedirectMatch ^(?!de) http://example.com/
If the context is .htaccess, for virtualhost It would be:
RedirectMatch ^/(?!de) http://example.com/
Note: I use /de/ originally because that's what you describe in your question, and also I match slashes in the target. Both source and target without slashes would be fine too for cases like /desomething or /de/something. In any case, always match slashes or the lack of them.
Note2: Do not use .htaccess to redirect unless you are not the admin of the site. It just complicates things and adds unnecessary overhead since the file/s need to be checked a number of times per hit.

Htaccess redirect wiout trailing slash is working but with it is not

I have a WordPress website. And I want to redirect a URL to another 3rd party domain. The issue is that destination domain cannot have trailing slash.
So I want to redirect http://www.domain.com/xyzxyzxyz to http://3rd-party-domain.com/?something
It's working fine for without trailing slash request urls
http://www.domain.com/xyzxyzxyz -> http://3rd-party-domain.com/?something
but when I try http://www.domain.com/xyzxyzxyz/ then it does not work because it adds trailing slash in destination URL too, which gives error.
I have tried these rewrite rules
Redirect 301 /xyzxyzxyz http://3rd-party-domain.com/?something
Redirect 301 /xyzxyzxyz/ http://3rd-party-domain.com/?something
Not working :(
Please let me know how to fix this.
Try RedirectMatch for its regex capabilities:
RedirectMatch 301 ^/xyzxyzxyz/?$ http://3rd-party-domain.com/?something