htaccess - need suggestion url rewriting [duplicate]

I am trying hard to rewrite this URL

http://localhost/sitecms/?search=viewPage&pageId=1

TO

http://localhost/sitecms/pages/page/1

N.B. Variable 'search' declared at the top inside index.php, viewPage as function declared inside index.php receives pageId variable, and pass to viewPage.php inside another folder(template) along with associated data.

Below is my .htaccess code.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pages/([^/]+)/?$ ?search=viewPage&pageId=$1 [L]

It's not working.

Suggestions are highly welcome about what I missed.


Solution 1:

With your shown samples, could you please try following. Please clear your browser cache before testing your URLs.

Options +FollowSymLinks
Options -MultiViews
RewriteEngine ON
##This rule redirects from http://localhost/sitecms/?search=viewPage&pageId=1 TO http://localhost/sitecms/pages/page/1
RewriteCond %{THE_REQUEST} \s/(sitecms)/\?search=viewPage&pageId=(\d+) [NC]
RewriteRule ^ /%1/pages/page/%2? [R=301,NE,L]

##Rules for handling non-existing files/directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(sitecms)/(?:[^/]*)/(?:[^/]*)/(\d+)/?$ $1/?search=viewPage&PageId=$2 [NC,L]