Search code examples
apache.htaccessmod-rewrite

How to mod rewrite for folder and subfolder


I'm using the code below to redirect the url below :

www.example.com/100

to

www.example.com/index.php?cod=100

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?cod=$1 [L] 

It works, but what if i want to redirect this url :

www.example.com/100/200

to :

www.example.com/index.php?cod=100&othercod=200


Solution

  • You'd need to add another rule which handles this more specific request before the rule you already have in place. Something like that:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # https://www.example.com/100/200
    RewriteRule ^(\d+)/(\d+)/?$ index.php?cod=$1&othercod=$2 [L] 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # https://www.example.com/100
    RewriteRule ^(\d+)/?$ index.php?cod=$1 [L]