Home > Mobile >  .htaccess two domains for languages ignore folder
.htaccess two domains for languages ignore folder

Time:09-14

Given this directory structure:

.
├── assets
│   └── img/
│       └── foo.png
├── en
│   └── index.php
└── fr
    └── index.php

There are two domains:

  english.com
  french.com

My goal is to redirect

english.com => english.com/en
french.com => french.com/fr
english.com/fr => french.com/fr
french.com/en => english.com/en

no redirects for:

   english.com/assets/img/foo.png
   french.com/assets/img/foo.png

The current solution is not working propery how to achieve the goals above

<IfModule mod_rewrite.c>
    RewriteEngine On

    # no www, https only
    RewriteCond %{SERVER_PORT} !^443$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST}$ 
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

    RewriteRule ^(en) - [L]
    RewriteRule ^(fr) - [L]
    RewriteRule ^(assets) - [L]

    redirectMatch 301 ^/en$ https://english.com/en
    redirectMatch 301 ^/fr$ https://french.com/fr

    RewriteCond %{HTTP_HOST}: englisch.com$ [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
  
    RewriteCond %{HTTP_HOST} french.com$ [NC]
    RewriteRule ^(.*)$ https://french.com/fr/$1 [R=301,L]
</IfModule>

CodePudding user response:

Have it like this:

RewriteEngine On

# no www, https only
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]

# ignore all rules for anything in assets/ directory
RewriteRule ^assets/ - [L,NC]

# for fr/ host name should be french.com
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule ^fr/ https://french.com%{REQUEST_URI} [R=302,L,NE]

# for en/ host name should be english.com
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule ^en/ https://eglish.com%{REQUEST_URI} [R=302,L,NE]

# for english.com URI must start with en/
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule !^en/ /en%{REQUEST_URI} [R=302,L,NE]

# for french.com URI must start with fr/
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule !^fr/ /fr%{REQUEST_URI} [R=302,L,NE]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

  • Related