Redirect URL without lang directory to default language

Redirect URL without lang directory to default language - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Redirect URL without lang directory to default language error on your web browser. Problem :


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

Solution :

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_rewrite RewriteRule instead (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.


We hope that this article has helped you resolve the htaccess, redirects, error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=