How to change link from my htaccess page

Asked

Viewed 373 times

0

My normal url is:

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

I wish so:

http://localhost/paginas/noticias/1

id 1 is generated by php, I don’t know how to get it this way in htaccess.

1 answer

1


Create the htaccess file inside the folder ./paginas and write the following contents:

RewriteEngine on

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

If you have pages other than.php news, then use it like 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 must activate mod_rewrite as explained in this link

  • It didn’t work, it did Not Found

  • @Dinho was an htaccess error, change RewriteCond %{REQUEST_FILENAME} -f for RewriteCond %{REQUEST_FILENAME} !-f, see that I edited the response with the corrected code.

  • It worked, but when I open the page gives error in php saying that: Notice: Undefined variable: id.

  • Will it be if this is because of htaccess?

  • @Dinho No I tested here a php like this: <?php&#xA;&#xA;echo $_GET['id']; and it worked, the error is in your php, it has nothing to do with .htaccess. How is your php?

  • <?php $noticiass = mysql_query("SELECT * FROM 10cms_noticias WHERE id = $id") or die(mysql_error()); $id = $_GET['id']; ? > <? php $i = 0; while($Edit = mysql_fetch_assoc($noticiass)){ $i++; ? > <? php echo $Edit['title']; ? > <? php } ?>

  • @Dinho If you open the direct url so it will also give error http://exemplo/paginas/noticias.php?id=8 the error is in your code, you set the variable after running the query, see that in the first select you already request the $id, but you only set it after, the right is <?php $id = $_GET['id']; $noticiass = mysql_query("SELECT * FROM 10cms_noticias WHERE id = $id") or die(mysql_error()); ?> <?php $i = 0; while($edit = mysql_fetch_assoc($noticiass)){ $i++; ?> <?php echo $edit['titulo']; ?> <?php } ?>

  • @Dinho Conseguiu?

  • It worked with her code, pulled everything, but I pulled a include from my top, and she ran out of style.

  • @Dinho This is because of the BASE, At each path directory the access level to CSS and JS changes the good thing is to create the folders at the root. This is already another problem, please create a question with details about how you use CSS and JS and talk about the problem with htaccess, tomorrow I answer, good night -- The problems of changing the link is already solved.

  • Good evening. Thanks for your help.

  • @Dinho noticed that you have several answers in your questions, but you did not accept any as correct. To accept an answer as correct click on the drawing that is next to the answer that helped you the most. To understand how Stackoverflow works see this link: http://answall.com/tour

  • I didn’t even know it, it was bad kkk

  • @Dinho has other questions besides mine. Don’t worry, until tomorrow.

Show 10 more comments

Browser other questions tagged

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