These codes dont work together... How can I integrate both codes?

# Block#1
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^search/(.+?)/(\d+)/?$ /search.php?q=$1&p=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?q=([^\s]+)&p=(\d+) [NC]
RewriteRule ^ /search/%1/%2? [R=302,L]

# Block#2
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^search/(.+?)/(\d+)/(\d+)/?$ /search.php?q=$1&p=$2&ty=$3 [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?q=([^\s]+)&p=(\d+)&ty=(\d+) [NC]
RewriteRule ^ /search/%1/%2/%3? [R=302,L]
  • It would be useful to explain what you are trying to do and include exactly what is not working, what output you are getting and what is expected.
    – MrWhite
    May 9, 2016 at 14:03

1 Answer 1


mod_rewrite directives execute top-down, so the most specific rules should come before the more general rules.

In your code above you need to reverse code Block#1 and Block#2. ie. check for 3 URL params before checking for 2 URL params.

Alternatively, match the exact URL (rather than just a prefix) in the CondPattern (2nd argument to the RewriteCond directive). Perhaps by matching a trailing space in the THE_REQUEST variable. eg ...&p=(\d+)\s [NC]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.