.htaccessurlurl-rewriting

URL rewrite issue on .htaccess


Good evening

I'm trying to do an .htaccess with a rewrite url

basically the "short link" will be

mysite.it/I-123456

and it must go to

mysite.it/join.php?invitation=123456

This is the part of the .htaccess involving it

RewriteEngine On
RewriteRule ^I-([0-9]+)\$ join.php?invitation=$1 [NC]

but if I type www.mysite.it/I-123456 I will still landing in the homepage...

this is the full .htaccess file

RewriteEngine On
RewriteCond %{HTTP_POST} !www\.mysite\.it$
RewriteRule (.*) http://www.mysite.it/$1 [R=301,L]

RewriteEngine On
RewriteRule ^I-([0-9]+)\$ join.php?invitation=$1 [NC]

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mysite.it[NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp|php|css)$ - [NC,F,L]

Options -Indexes

what I'm doing wrong?


Solution

  • The backslash in the matching pattern in your rule RewriteRule ^I-([0-9]+)\$ does not make any sense. It makes the pattern match strings the contain a literal ampersand character at that position which is not what you want.

    That would be the correct variant:

    RewriteRule ^I-([0-9]+)$ join.php?invitation=$1 [NC]
    

    I would suggest a few additional modifications:

    RewriteRule ^/?I-(\d+)/?$ join.php?invitation=$1
    

    You can play around a bit with that rule on a htaccess validator.


    Update: In the comment here you now say that this invitation code can contain digits and letters. In your question you only show letters and the rule you implemented also tried to only match digits. So I assumed the invitation code is a number.

    Given that we talk about an alphanumeric code you need to match such:

    RewriteRule ^/?I-([a-z0-9]+)/?$ join.php?invitation=$1
    

    Or:

    RewriteRule ^/?I-([\w\d]+)/?$ join.php?invitation=$1
    

    Here is the updated validator.