skip to Main Content

I’m hoping to get some help with an htaccess solution for the following redirects:

Case 1) the user enters domain.com it should redirect to domain.com/nl

Case 2) if the user enters domain.com/nl or domain.com/fr it should not redirect

Case 3) the user enters domain.com/some-path it should capture the url path and stick /nl in front of it, domain.com/nl/some-path

So far I’ve tried the following rule:

Redirect 301 ^((?!(?:)nl($)|(?:)fr($)).)*$ domain.com/nl/$1

However in Case 3, only the final character of the path is selected and not the whole ‘some-path’.

Using multiple rules is ok too, but can it be done in a single rule?

2

Answers


  1. It can be done in single rule like this:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} !s/+(?:nl|fr)[/?s] [NC]
    RewriteRule ^ /nl%{REQUEST_URI} [L,NE,R=301]
    
    Login or Signup to reply.
  2. Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##For adding nl to home/base page.
    RewriteRule ^/?$ http://%{HTTP_HOST}/nl [R=301,NE,L]
    
    ##For dealing with pages NOT starting with nl or fr here.
    RewriteCond %{REQUEST_URI} !^/(nl|fr) [NC]
    RewriteRule ^ /nl%{REQUEST_URI} [R=301,NE,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search