Search code examples
php.htaccess

How to Rewrite urls if not ends with language id or country id in .htaccess


How to Rewrite urls if not ends with language id or country id in .htaccess

in my case i want redirect links not ends with

home/(en | fr| ar)/(us| ma |ae | sa )

for exemple :

https://exemple.com/home/en/sa/...

https://exemple.com/home/es/sa/...

https://exemple.com/home/ar/...

I tried this:

RewriteRule ^home/!(en|ae|sa)/!(sa|ma|ae|sa)/(.*)$ /$1 [L,R=301,QSA]

but it didn't work

Thank you all


Solution

  • You may use this redirect rule:

    RewriteRule ^home/(?!(?:en|fr|ar)/(?:us|ma|ae|sa))([^/]+/[^/]+) /$1 [L,R=301,NC,NE]
    

    (?!(?:en|fr|ar)/(?:us|ma|ae|sa)) is a negative lookahead condition that will fail the URL match if /home is not followed by allowed language/country codes.