How to mod_rewrite to convert /foo/bar to ?foo=bar
-
I have WordPress installed on my website root. I created a child theme and a custom page that runs a php script. I need to pass arguments to the script through $_GET.
Everything works fine if I use the following URL:
www.mywebsite.com/detail?pg=2&row=20&filter=all&filtercity=foo,bar,baz
I am trying to use prettier URLs. The one above would look like:
www.mywebsite.com/pg/2/row/20/filter/all/filtercity/foo,bar,baz
I want to use MOD_REWRITE in .htaccess to rewrite the 2nd URL above to the 1st one.
My .htaccess currently looks like
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/filter/([a-zA-Z0-9]+)/?$ ?filter=$1 [R,L]
RewriteRule . /index.php [L]
</IfModule># END WordPress
The above works somewhat if I have only
https://www.mywebsite.com/filter/all
It rewrites it to
https://www.mywebsite.com?filter=all
I don’t know how to do the other variables (pg, row, filtercity).
The above also is not loading my css correctly. I believe the css is being rewritten to something else. I thought the two RewriteCond -f and -d would prevent existing files from being rewritten. However it is not working.
Any help would be appreciated.
Thank you.
- The topic ‘How to mod_rewrite to convert /foo/bar to ?foo=bar’ is closed to new replies.