Home > Software design >  htaccess language redirect when any language set
htaccess language redirect when any language set

Time:07-28

I have a website example.com, the default locale is English, I want to redirected to example.com/en If is not set a language.

All request without language argument should redirect to default language

sample :

example.com/blogpost/thisisapost

should redirect to :

example.com/en/blogpost/thisisapost

or

example.com/one/tow/three/etc

should redirect to :

example.com/en/one/tow/three/etc

The website Developed with Laravel.

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (. )/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

CodePudding user response:

You can create the redirect like this:

RewriteEngine On
rewriteRule ^/?(^((?!nl|en).)*$) https://example.com/en/$1 [R=301,L]

en and nl will not be matched now, but everything else will

You can see the result here

CodePudding user response:

You can insert this rule just below RewriteEngine On line to make it work:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(?![a-z]{2}/) /en%{REQUEST_URI} [L,NE,R=301]
  • Related