htaccess how to prioritize RewriteCond

We recently migrate our new website and we need to adjust htaccess to avoid 404 error with the search engine,

The website was a Multilanguage site we decided to use session and cookie to manage multi language instead of string.

We put together htaccess to make the changes

I have my htaccess code below

RewriteEngine On

RewriteRule ^([^/]+)/fr/?(.*)$ /$1/$2 [R=301,NE,L]
RewriteRule ^([^/]+)/en/?(.*)$ /$1/$2 [R=301,NE,L]
RewriteRule ^([^/]+)/es/?(.*)$ /$1/$2 [R=301,NE,L]
RewriteRule ^([^/]+)/pt/?(.*)$ /$1/$2 [R=301,NE,L]
RewriteRule ^([^/]+)/ar/?(.*)$ /$1/$2 [R=301,NE,L]

RewriteRule ^/?ar(.*)$ /$1 [L,R=301]
RewriteRule ^/?pt(.*)$ /$1 [L,R=301]
RewriteRule ^/?es(.*)$ /$1 [L,R=301]
RewriteRule ^/?fr(.*)$ /$1 [L,R=301]
RewriteRule ^/?en(.*)$ /$1 [L,R=301]

RewriteRule ^/?location(.*)/(.*).html$ /newlocation$1/$2.html$3 [R=301,NE,L]
RewriteRule ^/?ads/location(.*)/(.*).html$ /ads/newlocation$1/$2.html$3 [R=301,NE,L]

RewriteRule ^/?page(.*)/(.*).html$ /newpage$1/$2.html$3 [R=301,NE,L]
RewriteRule ^/?ads/page(.*)/(.*).html$ /ads/newpage$1/$2.html$3 [R=301,NE,L]

this code above works great ! but when I add this code below,

RewriteCond %{REQUEST_URI} !^/ads/
RewriteRule ^([^/]+)\.html?(.*)$ /ads/$1.html$2 [L,QSA]

Nothing is working anymore. Is there anything wrong with this?

1 answer

  • answered 2022-05-07 04:45 RavinderSingh13

    With your shown samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. I have put comments before new rules(at the bottom of your shown htaccess file), make sure these rules are there only. Place of rules is also very important in htaccess.

    RewriteEngine On
    
    RewriteRule ^([^/]+)/fr/?(.*)$ /$1/$2 [R=301,NE,L]
    RewriteRule ^([^/]+)/en/?(.*)$ /$1/$2 [R=301,NE,L]
    RewriteRule ^([^/]+)/es/?(.*)$ /$1/$2 [R=301,NE,L]
    RewriteRule ^([^/]+)/pt/?(.*)$ /$1/$2 [R=301,NE,L]
    RewriteRule ^([^/]+)/ar/?(.*)$ /$1/$2 [R=301,NE,L]
    
    RewriteRule ^/?ar(.*)$ /$1 [L,R=301]
    RewriteRule ^/?pt(.*)$ /$1 [L,R=301]
    RewriteRule ^/?es(.*)$ /$1 [L,R=301]
    RewriteRule ^/?fr(.*)$ /$1 [L,R=301]
    RewriteRule ^/?en(.*)$ /$1 [L,R=301]
    
    RewriteRule ^/?location(.*)/(.*).html$ /newlocation$1/$2.html$3 [R=301,NE,L]
    RewriteRule ^/?ads/location(.*)/(.*).html$ /ads/newlocation$1/$2.html$3 [R=301,NE,L]
    
    RewriteRule ^/?page(.*)/(.*).html$ /newpage$1/$2.html$3 [R=301,NE,L]
    RewriteRule ^/?ads/page(.*)/(.*).html$ /ads/newpage$1/$2.html$3 [R=301,NE,L]
    
    ##Your newly added rules fixed ones here....
    RewriteCond %{THE_REQUEST} !\s/ads/ [NC]
    RewriteRule ^([^/]+)\.html?(.*)$ /ads/$1.html$2 [L,QSA]
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum