Problem creating friendly URLs with htaccess and regular expressions

0

Greetings, I am creating a dynamic web page in which certain information is loaded using a variable $_GET . and the url remain of the type

      https://www.dominio.com/deals/deals.php?deal_id=483

where the variable deal_id is the ID of my product and with this I show the info on the page making a query mysql , such as the name, price, etc and all good in this part. The thing is that I read that using the file htaccess I can change the url to make it more search engine friendly for an SEO theme and place it as:

     https://www.dominio.com/deals/nombre-articulo

The issue is that I have tried several regular expressions and I do not achieve the desired results because the url shows no changes and it keeps showing the same even after including the regular expression in the htaccess and it is probably because it is not implement the changes, could you help me? , I used this expression

    RewriteRule ^deals/(\w+)$ deals.php?deal_id=$1

but I really do not know how to implement it, and especially if I can include the name of the product in URL in addition to the product ID , something like concatenating the name of the product to URL .

I thought about going through the parameter of the product name, even if I did not use it type:

   https://www.dominio.com/deals/deals.php?deal_id=483&nombre=nombre-producto/

but now how would be the regular expression to mask this URL ?

    
asked by Alvaro Santafe 22.12.2017 в 20:16
source

2 answers

1

The main problem: \w is an escape sequence for [0-9_a-zA-Z] .
The script ( - ) is not included in \w .

You could:

  • include it using [-\w]+
  • or something more generic, any character except a bar: [^/]+


Now, you did not define the format in which you want to pass the ID in the URL. It can be:

  • Format: https://www.dominio.com/deals/id/nombre-articulo

    RewriteRule ^deals/(\d+)/([^/]+)/?$ deals.php?deal_id=$1&nombre=$2 [NC,L,QSA]
    

  • Format: https://www.dominio.com/deals/nombre-articulo-id

    RewriteRule ^deals/([^/]+)-(\d+)/?$ deals.php?deal_id=$2&nombre=$1 [NC,L,QSA]
    

  • Or directly do not pass the id.
    Format: https://www.dominio.com/deals/nombre-articulo

    But that implies that in your script, when you do not receive any value in $_GET['deal_id'] , you will have to search the database for the id that corresponds to the name you received in $_GET['nombre'] . In which case, you should be careful and think about the implementation because maybe 2 products have the same name, or the name of a product can change (it will depend on your business rules).

    RewriteRule ^deals/([^/]+)/?$ deals.php?nombre=$1 [NC,L,QSA]
    
  • answered by 12.01.2018 в 11:01
    0

    Use this configuration in the .htaccess, it works great for me.

        RewriteEngine on
        RewriteRule ^p/(\w+)/?$ deals.php?deal_id=$1 [L]
        RewriteRule ^p/(\w+)/(\w+)$ deals.php?deal_id=$1&deal_nombre=$2 [L]
        RewriteRule ^p/(\w+)/(\w+)/(\w+)$ deals.php?deal_id=$1&deal_nombre=$2&otro_parametro=$3 [L]
    

    On this page use what you mention, link

    The safest thing is that they have a field in the database called link_post, where the title of the Article is stored in a format that does not contain spaces, accents, eñes or other strange characters.

        
    answered by 27.12.2017 в 18:34