I am developing a sports news platform, which consists of 7 different sports news registration tables, due to the design of my portal, of the main page, of which there are different designs in thumbnails, such as other medium and larger designs, I link my sports news platform which you will notice because I use it in order to use several sports news tables.
Now the news of each id
or new article registered in the database, the information on the data obtained from the URL is displayed, by means of:
if (isset($_GET['id'])) {
$urlOne = $_GET['id'];
}
$url = str_replace("/","",$urlOne);
After that, a condition is passed to the queryWHERE url=?
Now my problem is in the design of the friendly URLs , although it works perfectly, there is a small problem that is not so serious but perhaps in the future, for example when adding more categories to the URL
Note: I do not have a category system, but through
.htaccess
it it is easy to give the imagination that it does exist, even though it does not.
When adding categories to the url, I want to highlight it directly from the column url
of the sports news tables, so I don't have to modify my .htaccess
every time a new category exists and thus avoid doing the following:
RewriteRule ^mundial/([a-zA-Z0-9-/]+)$ detail?id=$1
RewriteRule ^noticias/deportivas/([a-zA-Z0-9-/]+)$ detail_one.php?id=$1
RewriteRule ^deportes/([a-zA-Z0-9-/]+)$ detail_two.php?id=$1
RewriteRule ^plus/noticias/([a-zA-Z0-9-/]+)$ detail_three.php?id=$1
RewriteRule ^news/noticias/([a-zA-Z0-9-/]+)$ detail_four.php?id=$1
RewriteRule ^futbol/([a-zA-Z0-9-/]+)$ detail_five.php?id=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)$ detail_six.php?id=$1
Of course, the modifications would not be a problem if I manage them by myself, but I am going to implement a reporting system in this system where users will add news, and they will insert
url
the url in the column, so that they can see that it is in accordance with their reporting, that is to say that the system will not generate a URL automatically but the users.
Now taking as a reference one of the answers to one of my questions and practicing with the code .htaccess
of the answer:
RewriteRule ^([^/]+)/([^/]+)/?$ detail?id=$2&cat=$1
I realized that that code works with a url, in the following way example.com/noticia/mi-url-del.post/
without having to add this to it .htaccess
: noticia
very different from what I used in my .htaccess
.
So my idea is the following, register the url to my idea, to my liking, in the column url
as follows:
id_sports title detail url ...
1 ...... ....... deportes/liga/futbol/español/otros/mas/categorias/mi-url-de-la-noticia/
And that when visiting it:
example.com/deportes/liga/futbol/español/otros/mas/categorias/mi-url-de-la-noticia/
The data of the news is displayed without any problem, now it is not always going to do 7 categories deportes/liga/futbol/español/otros/mas/categorias/
, it can be one, two, three or several.
So what I want is that the.htaccess
RewriteRule ^([^/]+)/([^/]+)/?$ detail?id=$2&cat=$1
Run the friendly URL, no matter how many category numbers exist in the URL , because this code RewriteRule ^([^/]+)/([^/]+)/?$ detail?id=$2&cat=$1
only works with one:example.com/noticia/mi-url-del.post/
The possibility exists since a single line of code
.htaccess RewriteRule ^([^/]+)/([^/]+)/?$ all_sport.php?id=$2&cat=$1
can execute one or two, or several categories without problem, or how many of them I must add in me .htaccess
so that there will be no errors, when visiting a news that more than one category has been added.
If you expect your php (detail.php or any other) to receive several parameters (cat1, cat2, cat3, ...) (one for each category), it cannot be done without knowing the maximum number of categories. It would imply having "infinite rules" (for 1 category, for 2, for 3, ... etc)
Perhaps you could work around this by setting a category limit, so you could write out all the redirects beforehand. But it's not very elegant.
However there is another alternative. The php, in the category parameter, instead of receiving one, could receive all of them (separated by "/"). And then in the php you separate by slash to obtain an array with all the categories.
For example, in the .htaccess you would have something like this:
What this will do is capture the categories in the first group ( ) and the url
$1
in the second ( ) ( See demo )$2
And in detail.php:
Example:
A user loads this address: example.com/deportes/liga/futbol/español/ otros/mas/categorias/mi-url-de-la-noticia/
Your apache, through the new htacess configuration, will examine the url and divide it into two parts:
Then it will pass this data to detail.php (for example) and the first part will be passed to the "categories" parameter and the second part to "url"
This is what your php can then receive through the variable
$_GET
.So in the php, with
$_GET['url']
you no longer have to do anything additional. But the categories will come to you "all together" in$_GET['categorias']
.If that's how you need it, perfect, you don't have to do anything else. But if instead you needed to pass those categories to an array, you could do it with the function
explode
, separating by/
Interpreting the URL from Apache with .htaccess
On the one hand, you have the file
.htaccess
, this file is Apache's way of allowing configuration changes at the directory level (this may vary depending on the administrator's configuration if you are not yourself).From this file, and thanks to the mod_rewrite apache module , you can create friendlier URLs.
RewriteRule
pattern > search regular expression
target > Substitution
flags > flags (modify behavior) https://httpd.apache.org/docs/2.4/rewrite/flags.html
Practical example
Taking your expression
([^\/]+)\/([^\/]+)\/?$
overexample.com/noticia/mi-url-del.post/
we get the following result:https://regex101.com/r/n7XyRc/1/
You can see from the link the result of the more detailed capture.
These two capturing groups are identified in the target as $1 and $2. Being sent to your php file that processes this information.