.htaccesssubdirectoryvanity-url

.htaccess vanity URLs - rewrite multiple GETs within subfolder


I've been trying to rewrite URLs within a subfolder, but without success. Nothing simply happens - no URL is rewritten at all. Please help!

I want this: www.example.com/fruits/?fruitName=apples&mode=buy

...to become this: www.example.com/fruits/apples/buy/

These rewrites will only occur in the /fruits/ folder. How is this done?

Here's the code that I have tried:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/fruits/$ /fruits/?fruitName=$1&mode=$2

Thank you!


Solution

  • You can use this code in your /fruits/.htaccess file:

    RewriteEngine On
    RewriteBase /fruits/
    
    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\w-]+)/([\w-]+)/?$ ?fruitName=$1&mode=$2 [L,QSA]