301 Redirect rule to cover multple cases - regex

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

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.

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.

RedirectMatch exclude certain directory

I'm using RedirectMatch 301 to redirect requests like mydomain.com/example to an example page. For that I'm using the following code in my .htaccess file:
RedirectMatch 301 (?i)/whatever http://thedomain.com
The (?i) is so that the redirect is case-insensitive. What I want to achieve now is that that only works when the /whatever is put directly after mydomain.com, so mydomain.com/sub/sub/whatever won't redirect the user.
Thanks in advance
Use regex anchors to restrict your match:
RedirectMatch 301 ^(?i)/whatever/?$ http://thedomain.com

How to redirect "/%20rel="?

The following redirect isn't working, I assume it's because of the characters in the page name (generated by Wordpress).
redirect 301 /%20rel= http://www.NewSite.com/OldSiteLandingPage.php
How can I get this 301 redirect from "/%20rel=" to work?
Replace your rule with this:
Redirect 301 "/ rel=" /

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]