I’ve added the end anchor to fetch the digits from the fourth atom but require them to be the last thing in the URI, the Last flag because it’s important (to mod_rewrite) and the QSA flag to retain any pre-existing query string.
As for your second question (foreigh characters), you’re not needing the $1 - $3 atoms so merely use ([^/]+) for each one. Otherwise, you’ve got to add the accented characters to a character range definition (e.g., [a-zA-Z0-9ã]+ to add your “ã” character). Obviously, more than one character makes this a ridiculous solution to the “NOT /” would be the preferred solution.
Okay, neither should work if you’re using Apache 2.x as the leading / in the RewriteRule will be rejected by mod_rewrite. The leading / in the redirection could also be redirected to the sites directory of the server’s root. In other words, if you’re using Apache 2, delete the leading / in the regex; if you’re using either (and the .htaccess is in the DocumentRoot of the domain), delete the leading in the redirection. Sorry I didn’t catch that earlier.
I used your first suggestion but I also changed the bits in between the /'s to [^/]+ as, like you said, I don’t need those atoms so this caters for all characters.
I am running Apache 2, and it seemed to work fine! This is on my localhost though where I’ve been testing it and I’ve put it in the httpd.config file. However, I’ve just found its not working on my live site (also running Apache 2), where I put it in the .htaccess file (in the public_html folder). I think its a problem with loading modules though, as when I put
LoadModule rewrite_module modules/mod_rewrite.so
at the top of the file, I get a server error. I’ll ask my host about that though.
The LoadModule directives are reserved for the httpd.conf (or equivalent). Use the test in my signature’s tutorial article to confirm that mod_rewrite is enabled and properly functioning.
The comment about the leading / in the regex was specifically aimed at Apache 2 installations:
Apache 1.x requires the leading / - ^/
Apache 2.x rejects the leading / - ^
Apache 1.x and 2.x accept - ^/?
The leading / in the redirection is interpreted FIRST as asking for a file relative to the server’s root directory THEN it will look to the DocumentRoot of the domain. Omitting the leading / in the redirection - especially when in the .htaccess in the DocumentRoot - is highly recommended.