Rewrite URL on .htaccess - regex

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.

Related

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.

RedirectMatch: redirects to wrong URL

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.

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.

.htaccess regex for redirectin specific url-endings

ATM we are investigating a bug in our webapp which produces duplicated urls for specific content. The duplicated urls get a suffix -X where X is an integer.
The right urls look like this:
http://www.foo.bar/entity/some-special-name
The duplicates look like this:
http://www.foo.bar/entity/some-special-name-1
Till we found the bug, we urgently need a workaround based on mod_rewrite which redirects the duplicate-urls to the originals. Has someone an idea, how I can achieve this kind of redirect? How would a regex look like? The redirect should only fire for urls in the /entity/ subdirectory.
You can use this redirect rule in your site root .htaccess to remove invalid URLs:
RedirectMatch 301 ^/(.+)-\d+/?$ /$1
If you just want to fix /entity/ URLs then use this rule:
RedirectMatch 301 ^(/entity/.+)-\d+/?$ /$1

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