How to hide.php displays from link (friendly url)

Asked

Viewed 673 times

3

All right? Please, I need help...

Currently, my site presents links as follows: https://site.com/pagina/noticia/2019/ciencia-e-saude/exibe.php?link=página-do-leitor-é-maneira

In the "science-and-health" folder, I have a file exibe.php which serves to display the content of a given link.

Look at the index that "pulls this file":

<a class="post-img" href="exibe.php?link=<?php echo $ciencia_e_saude['link'];?>"

I wish I could make a friendly URL that "hides" the "displays.php" of the page...

Actually, this is my ".htacess":

<files ~ "^.*\.([Hh][Tt][Aa])">
    order allow,deny
    deny from all
    satisfy all
</files>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1\.php
    RewriteRule ^([a-z,0-9,A-Z,_-]+)$ /exibe?link=


<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
</IfModule>

You can help me?

1 answer

1

The ideal is that for your site to become dynamic, you do not have "folders" of the categories. You see, ideally you only have the file displays.php at its root, and get all the URL parameters in this file. An example of your "unfriendly" URL would look like this: https://site.com/pagina/exibe.php?tipo=noticia&ano=2019&categoria=ciencia-e-saude&link=página-do-leitor-é-maneira

Note that each parameter of this url above will represent at the end a "/" in your URL. With the above model, your rewrite rule will look like this:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /pagina/exibe.php?tipo=$1&ano=$2&categoria=$3&link=$4 [L]

In your.php file, you can access each of these parameters through the $_GET variable: $ano = $_GET['ano']

$categoria = $_GET['categoria']

Another alternative would be to use some lib that allows the creation of routes, like that one

Browser other questions tagged

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