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?
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
\d
is the special character that matches any digit./?
allows the same, unmodified rule to be used inside the central configuration of the virtual host (which is the preferred place to implement such rules for various reasons). You can still use that rule in a distributed configuration file (".htaccess") as you currently do./?
allows for an optional trailing slash in requests. Some situations can lead to that, I see no reason to reject such a request.[NC]
flag does not really make sense in my eyes. You explicitly show the proposed final URL using a capital "I". So why match case insensitive? You might require [L]
flag or even an [END]
flag in that rule, though.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.