Redirect 301 Urls with id number htaccess - regex

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.

Related

Redirect 301 using regexp in .htaccess file

I am looking for a Regex to redirect urls like:
example.net/example-1
example.net/example-2
example.net/example-3
I have many URLs that have the same prefix, but a different suffix after example-.
I was trying around with regex.
Redirect 301 /example-.*$ http://example.net/example-overview
Redirect 301 /example-(.*)? http://example.net/example-overview
Redirect 301 /example-(.+)[/]?$ http://example.net/example-overview
Redirect 301 /([^/example-]+) http://example.net/example-overview
I can't get it work.
Normal redirects for example example-1 to example-overview are working fine.
EDIT:
Found a solution:
RedirectMatch 301 ^/example-(.+)[/]?$ http://example.net/example-overview
Redirect doesn't support regular expressions, use RedirectMatch for that.

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.

301 Redirect rule to cover multple cases

Is there any way I can do all the following ( and similar ) redirects with a rule
Redirect 301 brand/brand1 http://domain.com/department/brand1
Redirect 301 brand/brand2 http://domain.com/department/brand2
Redirect 301 brand/brand3 http://domain.com/department/brand3
Yes you can use RedirectMatch that gives your regex capabilities like this:
RedirectMatch 301 brand/([^/]+) http://domain.com/department/$1

Mod Rewrite with question mark in result

I have URLs that look like mysite.com/go/somevalue and I want to redirect them to mysite.com?shorturl=somevalue. Think of it as a branded url shortener.
I have tried the following among other incarnations. I think the problem is with the question mark in the results.
RedirectMatch 301 /go(.*) %3F$1
RedirectMatch 301 /go(.*) \?$1
RewriteRule ^.*/go/(.*)$ \?shorturl=$1
This RedirectMatch should work:
RedirectMatch 301 ^/go/(.+)$ /?$1
You don't want to escape the ? if you want tha tas part of the query string:
RewriteRule ^.*/go/(.*)$ ?shorturl=$1
The other two statements are mod_alias redirects, which will redirect the browser and cause the location bar to change. If that's what you want, you can add a [L,R=301] to the end of the rewrite rule and not have a need for the RedirectMatch lines

Redirect 301 /category/ /category/category/

I need to redirect the category to a sub category who have the same name of the category.
My code :
redirect 301 /abcd /abcd/abcd
But i've a redirect infinite.
/abcd/abcd/abcd/abcd/abcd/abcd/abcd/abcd/abcd/abcd.....
Same thing with that :
redirect 301 /abcd /abcd/efgh
I've got this :
/abcd/efgh/efgh/efgh/efgh/efgh/efgh/efgh/efgh/efgh.....
How can i do that?
Your regex /abcd is matching the rewritten URI also and causing this looping.
You should better use RedirectMatch that has regex support, so you do:
RedirectMatch 301 ^/abcd/?$ /abcd/efgh
You can try:
RewriteRule ^/abcd$ /abcd/abcd [R=301,L]