Home > OS >  Redirection with alphanumeric digit numbers
Redirection with alphanumeric digit numbers

Time:11-27

I need to redirect everything from the first "/" in my domain. For example

I need to redirect this: https://audiobookscloud.com/B07D5HYCR2

For that: https://example.com/B07D5HYCR2

But I need to ensure that this only happens when there are 10 digits after the first slash, that is: ".com/"

My solution was this:

RewriteRule ([^/] )/\d{10}?$ https://www.example.com/$1 [L,R=301]

But it doesn't work as expected.

How can I resolve this?

CodePudding user response:

Your matching pattern does not match what you describe. What you implemented is that: Any string that contains any non empty string that does not contain a slash, followed by a slash and nothing else or exactly 10 digits. A RewriteRule is only applied to the path component of a requested URL. So just to the /B07D5HYCR2 in your example and not to something like audiobookscloud.com/B07D5HYCR2, as you apparently expect.

Change the matching pattern slightly to come closer to what you describe:

RewriteRule ([^/]{10})?$ https://www.example.com/$1 [L,R=301]

Though that will redirect a few more URLs than you want, according to your description. Which is why I would recommend some further changes:

RewriteRule ^/?[0-9A-Z]{10}$ https://www.example.com%{REQUEST_URI} [L,R=301]

That variant precisely matches all request paths that contain exactly 10 characters that are either a digit or a capital letter, with nothing before or behind that.

I really recommend to take a look into the documentation of the tools you are trying to use. Here that would be the apache rewriting module. It is, as typical for OpenSource software, of excellent quality and comes with great examples: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

  • Related