David Cassidy
David Cassidy

Reputation: 99

Apache Mod_Rewrite help please

I am trying to write a mod_rewrite rule to fulfill the following conditions:

/hire/audio --> /section?section=audio
/hire/audio/speakers --> /section?section=audio&sub_section=speakers
/hire/audio/speakers/jbl-jrx112m --> /detail?section=audio&sub_section=speakers&product=jbl-jrx112m
/hire/audio/speakers/d&b-q7-top --> /detail?section=audio&sub_section=speakers&product=d&b-q7-top
/hire/lighting/generic-lighting/thomas-par64-floor-can--> /detail?section=lighting&sub_section=generic-lighting&product=thomas-par64-floor-can
/hire/audio/speakers/all --> /section?section=audio&sub_section=speakers&filter=1
/hire/audio/speakers/1 --> /section?section=audio&sub_section=speakers&filter=1
/hire/audio/speakers/2 --> /section?section=audio&sub_section=speakers&filter=2

The above list isn't exhaustive, but it gives you an idea of all the possible permutations I am aiming to cover.

My tried htaccess rules file:

RewriteRule "^/hire/([0-9a-zA-Z-&]+)/([0-9a-zA-Z-&]+)/([0-9a-zA-Z-&]+)" "/detail?section=$1&sub_section=$2&product=$3" [NC, L]

Upvotes: 1

Views: 72

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133750

With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rule to handle(/hire/audio --> /section?section=audio).
RewriteRule ^hire/(audio)/?$ /section?section=$1 [NC,L]

##Rule to handle(/hire/audio/speakers --> /section?section=audio&sub_section=speakers).
RewriteRule ^hire/(audio)/(speakers)/?$ /section?section=$1&sub_section=$2 [NC,L]

##Rule to handle(/hire/audio/speakers/jbl-jrx112m --> /detail?section=audio&sub_section=speakers&product=jbl-jrx112m)
RewriteRule ^hire/(audio)/(speakers)/(jbl-jrx112m)/?$ /section?section=$1&sub_section=$2&product=$3 [NC,L]

##Rule to handle(/hire/audio/speakers/d&b-q7-top --> /detail?section=audio&sub_section=speakers&product=d&b-q7-top)
RewriteRule ^hire/(audio)/(speakers)/(d&b-q7-top)/?$ /section?section=$1&sub_section=$2&product=$3 [NC,L]

##Rule to handle(/hire/lighting/generic-lighting/thomas-par64-floor-can--> /detail?section=lighting&sub_section=generic-lighting&product=thomas-par64-floor-can)
RewriteRule ^(lighting)/(generic-lighting)/(thomas-par64-floor-can)/?$ /detail?section=$1&sub_section=$2&product=$3 [NC,L]

##Rule to handle(/hire/audio/speakers/(1|2) --> /section?section=audio&sub_section=speakers&filter=1)
RewriteRule ^(hire)/(audio)/(speakers)/(\d+)/?$ /section?section=$2&sub_section=$3&filter=$4 [NC,L]

##Rule to handle(/hire/audio/speakers/all --> /section?section=audio&sub_section=speakers&filter=1)  
RewriteRule ^hire/(audio)/(speakers)/all/?$ /section?section=$1&sub_section=$2&filter=1 [NC,L]

NOTE: This considers that you have document(s)(which is serving from backend) in root, if not then remove first / from right side of each rule please.

Upvotes: 2

Related Questions