0

I need some help regarding .htaccess rewrite rule. One of my page have combination of following different values which need to be rewrite in different format.

http://mysite.com/test TO http://mysite.com/page.php http://mysite.com/test/$cat/$id TO http://mysite.com/page.php?cat=$cat&id=$id http://mysite.com/test/$cat TO http://mysite.com/page.php?cat=$cat http://mysite.com/test/$cat/&next=13 TO http://mysite.com/page.php?cat=$cat&next=$next 

I currently have following rule that only works for 2nd and 4th example

RewriteRule ^test/(.+)/(.+)/?$ page.php?cat=$1&id=$2 
5
  • Is your to and from the wrong way around in the question? Commented Sep 16, 2011 at 22:53
  • Do you have the rewrites backwards? The rule you have rewrites http://mysite.com/test/$cat/$id TO http://mysite.com/page.php?cat=$cat&id=$id, not the other way around. Commented Sep 16, 2011 at 22:54
  • How do $next and $id differ? Will $id ALWAYS be numeric only, and will $next ALWAYS contain non-numeric character(s)? Commented Sep 16, 2011 at 22:56
  • @TheCodeKing I am not sure, but I corrected it. Commented Sep 16, 2011 at 22:59
  • @Joe value for $next should be passed in &next=13 format at the end, actually I am using SmartyPaginate plugin and It doesn't let me change the link structure for $next, that's why I need to pass that value in default structure. Commented Sep 16, 2011 at 23:07

2 Answers 2

3

The first 3 look pretty straightforward, but I don't understand what you mean by the fourth one, what exactly is &next? Is that a query string? Do you only care about this &next when it =13? Here's a stab at guessing what you're looking for (order matters):

# First RewriteRule ^test$ page.php [L] # Fourth ?? RewriteRule ^test/([^/]+)/([^=]+)=(.+)$ page.php?cat=$1&$2=$3 [L] # Second RewriteRule ^test/([^/]+)/([^=]+)$ page.php?cat=$1&id=$2 [L] # Third RewriteRule ^test/(.+)$ page.php?cat=$1 [L] 

If the fourth one is supposed to be a query string, as in: http://mysite.com/test/foo/?bar=13 then you need to change the last rule by adding a QSA to the end:

RewriteRule ^test/(.+)$ page.php?cat=$1 [L,QSA] 

If I'm misunderstanding still, we can include the rule you have

# First RewriteRule ^test$ page.php [L] # Third RewriteRule ^test/([^/]+)$ page.php?cat=$1 [L] # Second and Fourth (your rule) RewriteRule ^test/(.+)/(.+)/?$ page.php?cat=$1&id=$2 [L] 

(you need the [L] at the end or else further rules get applied)

Sign up to request clarification or add additional context in comments.

Comments

1

These should work, creating separate rules for each case is a lot easier. I've had to make some assumptions on your URLs.

RewriteRule ^/test$ page.php RewriteRule ^/test/([^/]+)/([^&/]+)$ page.php?cat=$1&id=$2 RewriteRule ^/test/([^/]+)$ page.php?cat=$1 RewriteRule ^/test/([^/]+)/&next=([^/]+)$ page.php?cat=$1&next=$2 

Comments