0

I have the following RegEx in Javascript:

    var re1 = '(~)';
    var re2 = '([a-z])';
    var re3 = '([a-z])';
    var re4 = '(\\d)';
    var re5 = '([a-z])';
    var re6 = '(\\d)';
    var matchExp  = new RegExp(re1 + re2 + re3 + re4 + re5 + re6, ["i"]);
    var match = window.location.href.match(matchExp);
    var shortcode = match == null ? "" : match[1];

What I am trying here is shortcodes, I want any url such as /~Xx0X0 to be redirected to /create.html. Where X/x is a non-whitespace character and 0 is a digit.

How would I use the Regex in .htaccess mod_rewrite? I hope that I explained it all correctly.

Shortcode Format

  • ~

    Uppercase Character

    Lowercase Character

    Digit

    Uppercase Character

    Digit

1 Answer 1

1

You can put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^~[A-Z][a-z][0-9][A-Z][0-9]$ /create.html [L,R=301]

Reference: Apache mod_rewrite Introduction

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! would it be possible to limit the shortcode to 6 characters (including ~)?
As @tenub: commented it is already limited 6 character including ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.