How to change link of my page with htaccess

0

My normal url is:

http://localhost/paginas/noticias.php?id=1

I wanted it this way:

http://localhost/paginas/noticias/1

The id 1 is generated by php, I do not know how to make it look like this in htaccess.

    
asked by anonymous 24.07.2015 / 00:17

1 answer

1

Create the htaccess file inside the ./paginas folder and type the following content:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^noticias/([0-9]+)$ noticias.php?id=$1 [L]

If you have pages besides the news.php, then use this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9\-_]+)/([0-9]+)$ $1.php?id=$2 [L]

In this case you can access pages that will be equivalent:

  • http://exemplo/paginas/noticias/8 = > http://exemplo/paginas/noticias.php?id=8
  • http://exemplo/paginas/blog/2 = > http://exemplo/paginas/blog.php?id=2
  • http://exemplo/paginas/produto/4 = > http://exemplo/paginas/produto.php?id=4
  • http://exemplo/paginas/artigo/5 = > http://exemplo/paginas/artigo.php?id=5

Note that for this you should enable mod_rewrite as explained in this link

    
24.07.2015 / 05:02