Need your help in rewriting rule of url to be friendly

I need your help how I can rewrite my website’s page url to be friendly from this:

http://www.kwatza.com/calendar/?action=details&eventId=16

to this:

http:// www.kwatza.com/calendar/details/16

If you notice that “action” is equals to “details” which is the third action variables in the calendar folder. There are three variables (in-order) written in switch method in the calendar/index.php file. Each variables represents each page and these are:

view-calendar
list-monthly-events
details

I was able to find answers on the first two (view-calendar & list-monthly-events) by the following scripts

RewriteRule ^calendar/([a-z-]+)$ calendar/index.php?action=$1 [NC,L]
RewriteRule ^calendar/([a-z-]+)/([0-9]+)$ calendar/index.php?action=$1&city_id&m=$2 [NC,L]

I tried this script for the third “action=details” but it did not worked. It would show blank.

RewriteRule ^calendar/([a-z-]+)/([0-9]+)$ calendar/index.php?action=$1&eventId=$2 [NC,L]

Please help how I can rewrite the rule correctly and thank you very much in advance.

espi62

The problem you have is the following two rules use the same match. So you have conflicting rules.

RewriteRule ^calendar/([a-z-]+)/([0-9]+)$ calendar/index.php?action=$1&city_id&m=$2 [NC,L]

and
RewriteRule ^calendar/([a-z-]+)/([0-9]+)$ calendar/index.php?action=$1&eventId=$2 [NC,L]

Apache can’t determine which one to use, so it uses the first one it encounters. You need to find a way to separate them.

If details is the only one NOT working, I’d try this (as it makes the two match rules different):

RewriteRule ^calendar/([a-z-]+)$ calendar/index.php?action=$1 [NC,L] 
RewriteRule ^calendar/details/([0-9]+)$ calendar/index.php?action=details&eventId=$1 [NC,L]
RewriteRule ^calendar/([a-z-]+)/([0-9]+)$ calendar/index.php?action=$1&city_id&m=$2 [NC,L]

Wow! It worked. I have been looking for answers in days and you solved it in seconds

My biggest thanks!!!

espi62

espi62

While cp is quite correct that you can’t repeat a regex and expect Apache to guess which of three options it’s supposed to respond to, there is a much better way to do this cleanly (but ONLY if the second key/value pair isn’t changed): replace ([a-z]+) with a list, i.e., (view-calendar|list-monthly-events|details).

Unfortunately, you’ve elected to change from a null second key for details to city_id (without a value) and m for list-monthly-events to eventId for view-calendar (or did I get those backward?). That will require the [a-z]+ to be specified in all cases (to distinguish between redirection options).

Additionally, NEVER use the No Case flag when matching {REQUEST_URI} strings as they ARE case sensitive!

Regards,

DK

My many thanks for sharing your knowledge…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.