Search code examples
regex.htaccesscodeigniter

I need a regex to replace underscore by hyphen


I have a problem with my application. I use CodeIgniter for my project and it's forbidden to use hyphen in classname (underscores are allowed). But I want to separate words in URLs by hyphens. So I need to rewrite urls by a regex to replace underscores by hyphens in htaccess file. Ideas?

For example :

I have many links :

  • domain.tld/I_need_help

  • domain.tld/I_need_regex

I want to have :

  • domain.tld/I-need-help

  • domain.tld/I-need-regex

In htaccess file, there should be :

RewriteRule ^(.*)_(.*)$ /$1-$2

Solution

  • Ok after a long search. I have the solution :

    Go to Codeigniter Routes regex - using dashes in controller/method names