0

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.

denoise
  • 53
  • 6
  • So example.com/de/ works, but not example.com/de? Just to make it absolutely clear. – vidarlo Feb 04 '22 at 20:59
  • 1
    @vidarlo exactly – denoise Feb 04 '22 at 21:00
  • 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? – MrWhite Feb 05 '22 at 12:25
  • "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). – MrWhite Feb 05 '22 at 12:29

0 Answers0