Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirecting dynamic url

dynamic url, conditional htaccess rule

         

zaira_86

5:09 am on Aug 13, 2010 (gmt 0)

10+ Year Member



I have this set of dynamic url in my site

1. http:// www.mysite.com/links.php?catname=hello&subname=world&page=1
2. http:// www.mysite.com/links.php?catname=hello&subname=world
3. http:// www.mysite.com/links.php?catname=hello&page=1
4. http:// www.mysite.com/links.php?catname=hello


I have this following rule to redirect that url's above

# 1. if catname,subname, page is requested
RewriteRule ^category/([^/]+)/([^/]+)/page-([^/]+).html$ links.php?catname=$1&subname=$2&page=$3 [nc]

# 2. if both catname and subname is requested
RewriteRule ^category/([^/]+)/([^/]+).html$ links.php?catname=$1&subname=$2 [nc]

# 3. if both catname and page is requested
RewriteRule ^category/([^/]+)/page-([^/]+).html$ links.php?catname=$1&page=$2

# 4. if catname only
RewriteRule ^category/([^/]+).html$ links.php?catname=$1 [nc]


Now, my rule #2 and #3 wont work, somewhat the rule is in conflict because they have the same number of trailing slash which is equal to 2.
How do i write the htaccess rule for the 4 links above so it wont get in conflict with the other rule? Is, if statement possible in htaccess? Any help, please?

jdMorgan

6:41 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Put your most-specific rules first.
2) Always use an [L] flag, unless you know why you should not.
3) Use proper regex patterns for the desired match.
4) Escape all periods (and all other regex matching- and quantifier tokens) in regular expressions patterns if you wish to match the literal character. Unescaped, the "." token matches any single character.
5) Be sure that MultiViews are disabled unless your site requires content-negotiation.
6) Delete your browser cache before testing any new code on the server.

# Was rule #1. catname, subname, page requested
RewriteRule ^category/([^/]+)/([^/]+)/page-([^/.]+)\.html$ links.php?catname=$1&subname=$2&page=$3 [NC,L]

# Was rule #3. both catname and page requested
RewriteRule ^category/([^/]+)/page-([^/.]+)\.html$ links.php?catname=$1&page=$2 [NC,L]

# Was rule #2. both catname and subname requested
RewriteRule ^category/([^/]+)/([^/.]+)\.html$ links.php?catname=$1&subname=$2 [NC,L]

# Was rule #4. catname only
RewriteRule ^category/([^/.]+)\.html$ links.php?catname=$1 [NC,L]

Jim