Search code examples
regex.htaccesscyrillic

Regular expression to match russian, allow all cyrillic characters in .htaccess


How do i redirect url with russian slug to specific php page. For example I have this url.

   http://www.example.com/основной-момент.htm

and want to redirect to this one in .htaccess

   http://www.example.com/category.php?slug=<russian slug>

Solution

  • If it's allowed in your server you could try something like this for the specific page in your .htaccess file:

    RewriteEngine On
    RewriteRule ^основной-момент.htm$ category.php?slug=ru
    

    In Regex if the character set is enabled on your server you should be able to use the range for the characters:

    RewriteRule ^([A-Яа-я-]+)\.htm$ category.php?slug=ru
    

    To capture the phrase you would use this (just like English):

    RewriteRule ^([A-Яа-я-]+)\.htm$ category.php?slug=$1
    

    Another way is by detecting the language

    #For users with Russian as their primary browsing language
    RewriteCond %{HTTP:Accept-Language} ^ru [NC]
    RewriteRule .* category.php?slug=ru
    

    Additionally if you're using a dynamic language on the server you should be able to use the REQUEST_URI var for parsing and determining the intended language as Apache serves the content and programming languages (like PHP or Perl) can do more with parsing.