Url rewriting using RewriteCond whrere different url required based on query param

Viewed 32

My requirement is

  1. domain.com/src/home.php?page=category&category=recipe
  2. domain.com/src/home.php?page=article&key=myarticle
  3. domain.com/src/home.php

Expected end url

  1. domain.com/src/category/recipe

  2. domain.com/src/article/myarticle

  3. domain.com

I have written one

RewriteRule    ^src/([A-Za-z0-9-]+)/?$    src/home.php?page=article&key=$1    [NC,L] 

The problem is if one is working then the other is not. Can anyone please suggest how to make it work with some condition based on "page" query param.

Thanks in advance.

1 Answers

With your shown samples, attempts, please try following htaccess Rules file. Make sure to keep your htaccess file along with src folder(not inside it).

Also please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]

##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]

##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]
Related