htaccess redirect 301 without a separator - regex

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.

Related

Redirect 301 Urls with id number htaccess

I have urls like this
https://www.example.co.uk/382-princeton-teak-6ft-garden-bench-chinoiserie-bench.html
That i want to redirect to
https://www.example.co.uk/princeton-teak-6ft-garden-bench-chinoiserie-bench.html
I tried with RedirectMatch 301 ^/(.+?)([0-9]+)-(.*)\.html$ /$1.html it do match but it redirects to https://www.example.co.uk/3.html any thoughts guys ?
You should be using this rule as number is right at the start:
RedirectMatch 301 ^/[0-9]+-(.+\.html)$ /$1
Clear your browser cache to test 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.

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 without the path

I have the following htaccess rule:
Redirect 301 / http://www.example.co.uk/blog/
On an old blog at http://blog.example.co.uk/ that should be redirecting ALL urls from this old blog to the new one.
However if I have anything in the path: e.g. http://blog.example.co.uk/2015/test-post then it redirects to http://www.example.co.uk/blog/2015/test-post
How do I make it so that it doesn't keep the path and just redirects to the domain.
You need to use RedirectMatch for that:
RedirectMatch 301 ^ http://www.example.co.uk/blog/
Make sure to test this in a new browser.
Or if you want to strip off any existing query string also then use mod_rewrite:
RewriteEngine On
RewriteRule ^ http://www.example.co.uk/blog/? [L,R=301]

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