htaccess replace "_" with "-"

Asked

Viewed 89 times

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 is as follows:

http://domain.com/como_funciona

I’d like to leave you like this:

http://domain.com/como-funciona

It is possible to do this with . htaccess?

1 answer

0


htaccess will not change your direct URL in the browser, it will remain the same. Internally it will convert 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_para_modify, in PHP when you do the capture, it will return like this:

<?php

var_dump($_GET);

Browser output:

inserir a descrição da imagem aqui

  • But I’ll get through - instead of _ ? this is my doubt.

  • 1

    Yes! is recommended by the good practices send in the url with the "-" tab instead of "_".

  • my_url_para_modify is a correct method? If I pass the localhost/test/my-url-to-modify url it finds the method?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.