Redirect URL without lang directory to default language
This: example.com/test/ should redirect to: example.com/en/test/, but: example.com/es/test/ shoudn't redirect.
in this case /test/ could be anything, with any number of levels (i.e.: /test/succeeded/ or / ).
It's an apache server, with .htaccess.
...
RewriteEngine on
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api$ api/ [L]
RewriteRule ^api/(.*)$ %ENV:REWRITEBASEwebservice/dispatcher.php?url=$1 [QSA,L]
RewriteRule ^images_ie/?([^/]+).(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]
....
#At the end of the file I have some redirects:
RedirectMatch 301 ^/es/manufacturer/(.*)$ /es/bodegas/$1
RedirectMatch 301 ^/en/manufacturer/(.*)$ /en/wineries/$1
Edit: Some asked what I've tried, and at the time of the question I hadn't tried because my regex knowledge is that bad. But thanks to a response I've tried some things:
RewriteRule !^(en|es)/(.*)$ /es/$1 [R=302,L] # From the response
RedirectMatch 302 !^/(en|es)/(.*)$ /es/$1 #Tried with a redirectmatch and adding starting / didn't work either
Try something like the following, using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteRule !^(en|es)/ /en%REQUEST_URI [R=302,L]
This states that for any requested URL that does not start /en/ or /es/ then redirect (302 - temporary) to /en/<requested-url>.
(Only change to a 301 - if that is what's required - once you have tested that it works OK.)
UPDATE:
Also REQUEST_URI appended index.php to the end of the url.
If you are seeing index.php in the URL then the mostly likely causes for this are either:
You've put the directive in the wrong place, ie. too late in the file. Any redirects need to go near the top of the file, before any existing rewrites. The order is important.
You have a conflict with another (possibly a mod_alias) directive.
Since you've updated the question I see you have redirects/rewrites from both mod_rewrite (
RewriteRule) and mod_alias (RedirectMatch). Different modules execute independently and at different times throughout the request. mod_rewrite will always execute first, despite the apparent order in the file. This can lead to (unexpected) conflicts. Ideally, you need to convert all your mod_alias redirects to use mod_rewriteRewriteRuleinstead (and make sure they are then in the correct order).
RewriteRule !^(en|es)/(.*)$ /es/$1 [R=302,L] # From the response
This would never work as expected, since you can't have capturing groups in a negated regex, they will simply never match anything and the $1 backreference will always be empty.
Aside: Also, Apache does not support line-end comments (ie. # From the response at the end of the directive on the same line). It appears to work OK in this instance because of the way Apache processes the directives (everything after a valid directive is simply ignored). But if you were to remove the 3rd argument (ie. the flags [R=302,L]) - which is still perfectly valid - then you'd get a 500 Internal Server Error. This is because Apache will then try to parse the "comment" as the flags argument which is wholly invalid.
Comments
Post a Comment