Regular Expression - 301 redirect - regex

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

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.

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 rule for changing part of url

I am trying to write an htaccess conditional redirect rule, but with no luck so far.
How can I use an htaccess redirect to redirect
http://www.domain.tld/en/oldpath/something/
http://www.domain.tld/en/oldpath/somethingelse/
to, respectively
http://www.domain.tld/en/newpath/something/
http://www.domain.tld/en/oldpath/somethingelse/
?
Is there a rule I can use to redirect "oldpath" to "newpath" whatever part of an url precedes or follows it?
Thank you for your help.
You can use:
RedirectMatch 302 ^(.*)/oldpath/(.*)$ $1/newpath/$2

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