Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

dynamic url change issue


I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/

My Current htaccess code :

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]    
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]

It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/ previously I removed the .php extension from the URL.

I tried a lot of solutions but nothing works. please help me


Solution

  • With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine On
    ##https apply rules here.
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    ##External redirect Rule here.
    RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
    RewriteRule ^ /%1/%2? [L,R=301]
    
    ##Internal rewrite rule here.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]