RedirectMatch exclude certain directory - regex

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

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.

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.

Regular Expression - 301 redirect

I have the below URL
http://www.yyyyy.com/en/x/y/2010/2010-02-03-test
And I want to redirect it to
https://www.yyyyy.com/x/y/2010-02-03-test
So what I need to do is, remove the /en/ part from the URL
I need the regular expression to add on .htaccess file that can help me to do 301 redirects for this,
You can use the following redirect:
RedirectMatch ^/en/x/y/2010/(.+)$ /x/y/$1

Redirect pages which end in a nubmer

This is such a stupid question, I'm so close to solving it but regex is killing me – I'm using the JavaScript history api on various pages of a test.
Server side, I would like to redirect:
/test/1
/test/2
/test/3
All to
/test
So I am using
Redirect 301 /test/1 /test
Redirect 301 /test/2 /test
Redirect 301 /test/3 /test
This all works, but I would like to redirect
Redirect 301 /test/1000 /test
So, here is where I am going wrong, after some hacking, googling etc I'm using:
Redirect 301 /test/([0-9]+) /test
to replace all other lines of code, but it's totally failing. What's the line to match all /test/{number} so I can redirect them all at once?
Redirect doesn't support regex, but RedirectMatch does: httpd://apache.org/docs/current/mod/mod_alias.html#redirectmat‌​ch
It would read:
RedirectMatch 301 /test/[0-9]+ /test
Redirect directive doesn't support Regular Expressions. Instead you can try RewriteRule:
RewriteRule ^test/[0-9]+$ /test [R=301,L]
Looks like this expression will work for you.
/(?:\d*)?\d+/g
http://regexr.com/3e8bk