Score:0

Language based RewriteRule like mysite.com/en/ in .htaccess

us flag

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 didn't found what I'm looking for.

vidarlo avatar
ar flag
So example.com/de/ works, but not example.com/de? Just to make it absolutely clear.
denoise avatar
us flag
@vidarlo exactly
kz flag
To clarify, the language "folder" is not a physical directory? "Every request in a language folder" - is this literally every request (as per your directive), or just `.php` files that exist at the following URL-path? What about static resources (CSS, JS, images, etc.)? Are these also in a language "folder"? Or should they be excluded? "Redirect any request without language path" - literally "any request"? No exceptions? What about static resources?
kz flag
"Add a trailing slash" - only after the "language folder" (as per your example), or should this be more general (although from your other examples I don't think this can be any more general). Should requests for `/en/` be rewritten to `index.php`? In your example you are rewriting this to literally `?lang=XX` (which is not strictly a valid end-point), this requires mod_dir issuing a subrequest for the `DirectoryIndex` (which is not explicitly set here, so is not guaranteed to work with the code as posted).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.