Search code examples
javascript.htaccessmod-rewriteurl-rewritingget

URL rewrite and get param is not working as expected


I have a form that send param to a page.

But I also have URL rewrite

In Htaccess I have

#mon chantier
RewriteRule mon-chantier/([^/]*)/([^/]*)\.html$ /index.php?page=mon-chantier&cp=$1&devis=$2 

When I send the form the URI is

mon-chantier?devis=eree&cp=eeee

and the URI I need to have is

mon-chantier/eree/eeee.html

I've tried with JavaScript to rewrite the URL with the input values, but if somebody disables JavaScript it does not work.


Solution

  • You can use this redirect rule at top of your .htaccess:

    RewriteEngine On
    
    # mon-chantier?devis=eree&cp=eeee -> mon-chantier/eree/eeee.html
    RewriteCond %{THE_REQUEST} /(mon-chantier)\?devis=([^\s&]+)&cp=([^\s&]+) [NC]
    RewriteRule ^ /%1/%2/%3.html? [R=302,L,NE]
    
    #mon chantier
    RewriteRule ^mon-chantier/([^/]*)/([^/]*)\.html$ /index.php?page=mon-chantier&cp=$1&devis=$2 [L,QSA,NC]