Redirect pages which end in a nubmer - regex

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

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: 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.

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

Negate user space URL in Apache

I have a situation where I am trying to use Apache's RedirectMatch directive to redirect all users to a HTTPS URL except a single (Linux) user accessing there own webspace. I have found a piece of regular expression code that negates the username:
^((?!andy).)*$
but when I try using it in the directive:
RedirectMatch ^((?!andy).)*$ https://www.example.com/
the URL:
http://www.example.com/~andy/
still gets redirected to the HTTPS URL:
https://www.example.com/
When I want it to ignore the redirection.
I am not an expert in regular expressions (or Apache), so any help in getting this working would be greatly appreciated.
Thanks,
Stephen
Edit:
OK, the plot thickens... if I comment out the line:
# RedirectMatch ^((?!andy).)*$ https://www.example.com/
and restart Apache, and then try the URL:
http://www.example.com/andy
I get a 404 response, which is expected. But, if I try the URL:
http://www.example.com/~andy
The redirect is triggered and I am redirected to:
https://www.example.com/
and as I said, the redirect is commented out and I have restarted the server. How can this happen? So this is not just a regex thing, it seems Apache is redirecting without instruction!
Something along the lines of this maybe? (Uses mod_rewrite instead of mod_alias)
RewriteCond %{REQUEST_URI} !^/~andy(/|$) ...
RewriteCond %{SCRIPT_URI} ^http:
RewriteRule ^/(.*) https://mysite.com/$1 [R=307]