htaccess switch "_" to "-"

1

I have this code that I'm using in my MVC that I did for studies:

class Como_Funciona extends Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        $data = [
            'title' => SITE_TITLE . ' - ' . SITE_SUBTITLE,
            'brand' => SITE_BRAND,
        ];

        $this->_view->render_template('header', $data);
        $this->_view->render_template('navbar', $data);
        $this->_view->render_template('pages/faq', $data);
        $this->_view->render_template('footer', $data);
    }
}

My url looks like this:

http://domain.com/como_funciona

I would like to leave it like this:

http://domain.com/como-funciona

Is it possible to do this with .htaccess?

    
asked by anonymous 20.08.2017 / 17:23

1 answer

1

htaccess will not change its direct URL in the browser, it will remain the same. Internally it will do the conversion of the characters according to your criteria. For example:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^_]*)_(.*)$ $1-$2 [L]
    RewriteRule ^(.*)$ index.php?router=$1 [QSA,L]
</IfModule>

When you pass the URL "my_url_to_modify, in PHP when you do the capture, it will return like this:

<?php

var_dump($_GET);

Browser output:

    
21.08.2017 / 15:54