skip to Main Content

I have a small translation system and works fine. When user clicks on the language the website redirects to /en or /pt.
I also have some custom rules on my htaccess file.

Options -Indexes

RewriteEngine On
RewriteBase /
RewriteRule     ^send-email$        send-msg-ajax.php [NC]
RewriteRule     ^cv$                docs/cv.pdf [NC]
RewriteRule     ^([a-zA-z_-]+)$     index.php?lng=$1 [QSA,L]


#custom error pages
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php

Everything works fine but 403 error appends the parameter lng to the url.

example.local/imgs/?lng=imgs

How can i make this whithout the ?lng=imgs?

thanks in advance!

2

Answers


  1. The correct code is here

    Options -Indexes
    RewriteEngine On
    RewriteBase /
    
    # Exclude certain directories from RewriteRule
    RewriteCond %{REQUEST_URI} !^/imgs
    # Exclude error pages
    RewriteCond %{REQUEST_URI} !^/error.php
    # Custom rules
    RewriteRule     ^send-email$        send-msg-ajax.php [NC]
    RewriteRule     ^cv$                docs/cv.pdf [NC]
    
    # Language rewrite
    RewriteRule     ^([a-zA-z_-]+)$     index.php?lng=$1 [QSA,L]
    
    # Custom error pages
    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    
    Login or Signup to reply.
  2. Please check this rule

    Options -Indexes
    RewriteEngine On
    RewriteBase /
    
    # Add a RewriteCond to exclude the "lng" parameter for error documents
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-zA-z_-]+)$ index.php?lng=$1 [QSA,L]
    
    RewriteRule ^send-email$ send-msg-ajax.php [NC]
    RewriteRule ^cv$ docs/cv.pdf [NC]
    
    # Custom error pages
    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    

    I have added a RewriteCond condition to check if the requested file doesn’t exist
    (using %{REQUEST_FILENAME} !-f). This condition ensures that the "lng" parameter won’t be appended to the URL for error documents, such as the 403 error page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search