1

I am trying to create mod_rewrite rules to redirect the following url in my wordpress site:

www.mywebsite.com/pg/2/row/20/filter/all/filtercity/foo,bar

to

www.mywebsite.com/detail?pg=2&row=20&filter=all&filtercity=foo,bar

My problem is that wordpress included the following rules in the .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I tried the following RewriteRule and it kind of works if I comment the last RewrireRule from Wordpress.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]]
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]
</IfModule>

# END WordPress

The above works only for urls like

www.mywebsite.com/pg/2 => www.mywebsite.com?pg=2

A few questions:

1 - how can I configure .htaccess to process the url only when it finds the following keys

/pg and/or /row and/or /filter and/or /filtercity

for all other urls, it should execute the standard Wordpress RewriteRule . /index.php

2 - also the url can have all of the keys or only a few of them. For example:

/pg/2/row/20

/row/20/filter/all

how can I configure the .htaccess to process all the different combinations of keys?

3 - when I tried replacing

RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]

with

RewriteRule ^pg/([a-zA-Z0-9]+)/?$ detail?pg=$1 [R]

I got a 404 error from the server. Not sure why. Any ideas?

Thank you.

EDIT 1:

I tried adding [L] to the end of my RewriteRule (see below)

RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$1 [R,L]

and moving the standard Wordpress rewrite rule to the next line

RewriteRule . /index.php [L]

After doing that I no longer get server 404 error. However the CSS files stop loading.

1 Answer 1

0

1 - how can I configure .htaccess to process the url only when it finds the following keys

/pg and/or /row and/or /filter and/or /filtercity

Read this Rewrite URL with .htaccess for multiple parameters

The above answer helps you to solve your problem when have a standard url format.

2 - also the url can have all of the keys or only a few of them. For example:

/pg/2/row/20

/row/20/filter/all

You are trying to do something out of URL semantics. When you try to access a resource, the path should be same every time. You can pass pg/0/ if you are on the starting page. But who can stop you from going out of URL semantics? :) In that case you have to have all the combinations as rules in your .htaccess. Follow the above solution, with all URL combinations it will work.

My Suggestions:

  • Don't Struggle with .htaccess if you are not familiar and your need is only URL rewrite (it is powerful though)
  • Pass everything to the index file and parse in your code, for your case have a look at this page http://codex.wordpress.org/Query_Overview
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.