Storsey
Storsey

Reputation: 299

htaccess rewrite folders to query string in MODx

I'm trying to mod_rewrite a URL folder structure to a query string internally but retain the original URL... ie. http://www.mysite.com/employers/events/2013/02
becomes:
http://www.mysite.com/employers/events/?e_year=2013&e_month=02
...internally. From the browser I'd like to see the original folder-based URL. My efforts have the URL being rewritten properly but the browser shows the query string URL instead. My code is as follows:

RewriteEngine On

RewriteRule ^employers/events/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /employers/events/?e_year=$1&e_month=$2 [L]
RewriteRule ^employers/events/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ /employers/events/?e_year=$1&e_month=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The second rule is just to catch the version with a trailing slash.

Many thanks in advance, this is causing me great pain.

Update:

This is what's being passed:

Array ( [q] => employers/events/ [e_year] => 2013 [e_month] => 02 )

So MODx is clearly sabotaging things internally.. not the best news but a bit of closure at least!

Upvotes: 0

Views: 627

Answers (1)

Phil
Phil

Reputation: 164764

  1. RewriteBase / does nothing if the .htaccess file is in the document root. Remove it
  2. I don't think you need the forward-slash prefix on your substitution. Also, try this single rule

    RewriteRule ^employers/events/(\d{4})/(\d{2})/? employers/events/?e_year=$1&e_month=$2 [L,QSA]
    

I've tested this locally and it works fine using the following .htaccess file

RewriteEngine on
RewriteRule ^a/b/(\d{4})/(\d{2})/? /a/b/?y=$1&m=$2 [L,QSA]

Update

Using a stand-alone example, your rewrite rules should result in the following query params being passed to the MODx index.php file

Array
(
    [q] => employers/events/
    [e_year] => 2013
    [e_month] => 02
)

What MODx does with this, I can't say

Upvotes: 1

Related Questions