Mod rewrite to get expression in url as parameter

I am trying to rewrite a url. I want to enter this url in the browser:

 localhost/subDir/page-name/value1/value2 

and then read value1 en value 2 into a querystring, so like this:

Read More
localhost/subDir/page-name/?firstArg=value1&secondArg=value2

This is for a wordpress website. The wordpress is installed in a subdirectory, called “subDir”. The wordpress page is called
page-name” and going to localhost/subDir/page-name works. The standard permalinks are working, so mod_rewrite is enabled. This is my .htacces file

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /subDir/

RewriteRule page-name/([^/]+)/([^/]+)/? page-name/?firstArg=$1&secondArg=$2 [QSA]
RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /subDir/index.php [L,QSA]

</IfModule>

# END WordPress

Related posts

Leave a Reply

2 comments

  1. mod_rewrite does not consider the query string as part of the URI for matching
    and rewriting, It should be treated separately.

    RewriteCond "%{QUERY_STRING}" "^firstArag=([^=]*)&secondArg=([^=]*)
    RewriteRule "/subDir/index.php" "http://localhost/subDir/page-name/%1/%2" [R]
    

    [R] indicates to direct to URL constructed by the RewriteRule directive

  2. You can replace your current code by this one

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /subDir/
    
       RewriteRule ^page-name/([^/]+)/([^/]+)/?$ page-name/?firstArg=$1&secondArg=$2 [L]
       RewriteRule ^index.php$ - [L]
    
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule ^ index.php [L,QSA]
    </IfModule>
    # END WordPress