简体   繁体   中英

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:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM