skip to Main Content

I’m trying to write redirect directives in the .htaccess to forward internally all user requests like this:

  • Every request in a language folder should redirect to the requested file with the language query string:
example.com/en/contact.php -> example.com/contact.php?lang=en
  • Redirect any request without language path to a default language folder like this:
example.com -> example.com/en
  • Remove trailing slash if the address is entered with it:
example.com/en/ to example.com/en
  • For the folder projects, every request should lead to the view-project.php file with the respective query strings:
example.com/en/projects/test -> example.com/view-project.php?lang=en&path=test

Here is my attempt, but it’s not working without trailing slash on a request like: http://www.example.com/de and is not redirecting http://www.example.com to a default language folder.

RewriteEngine On

RewriteRule ^(en|de)/(.*)$ $2?lang=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^projects/([^/.]+)/?$ view-project.php?path=$1 [QSA,L]

How can I achieve this?

This is possible a duplicate and I apologize for that. I searched everywhere and read about 100 posts, but I did’t found what I’m looking for.

2

Answers


  1. Chosen as BEST ANSWER

    after struggling a while and with the help of someone else, here is the .htaccess file that works for me:

    RewriteBase /example.com/
    DirectorySlash Off
    RewriteEngine On
    RewriteOptions AllowNoSlash
    
    RewriteRule ^$ de [R=301,L]
    
    RewriteRule ^(de|en)/$ $1 [R=301,L]
    
    RewriteRule ^(de|en)$ index.php?lang=$1 [L]
    
    RewriteRule ^(de|en)/projects/(.+) view-project.php?lang=$1&path=$2 [L,QSA]
    
    RewriteRule ^(de|en)/(.+) $2?lang=$1 [L,QSA]
    

  2. Try:

    RewriteEngine On
    RewriteBase /mysite.com/
    
    RewriteCond %{HTTP:Accept-Language} (en|de) [NC]
    RewriteRule ^ /%1/index.php [L]
    
    RewriteCond %{HTTP:Accept-Language} (en|de)
    RewriteCond %{DOCUMENT_ROOT}%1%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteRule ^(en|de)/(.+)$ $2&lang=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^projects/([^/.]+)/?$ view-project.php?path=$1 [QSA,L]
    

    Edit:

    The above was before the detailed explanation: After the detailed explanation, I’ve come up with this solution which is very similar to the solution the author has come up with while I was not aware about the edit explaining the situation:

    DirectorySlash Off # disables mod_dir slash redirect
    
    RewriteEngine On
    
    RewriteBase /mysite.com/ # rewrites inside /mysite.com/
    
    RewriteOptions AllowNoSlash # stops ignoring the directory redirects that redirect directories without slash, so by default: example.com/dir1 -> example.com/dir2 would be ignored. This is used because DirectorySlash is off
    
    RewriteRule ^(en|de)/projects/(.+)$ view-project.php?lang=$1&path=$2 [QSA,L] # rule 4
    RewriteRule ^(en|de)/(.*)$ $2?lang=$1 [QSA,L] # rule 1
    RewriteRule ^$ en [R=302,L] # rule 2
    RewriteRule ^(en|de)/$ $1 [R=302,L] # rule 3
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search