Apache – Using multiple rewrite rules

.htaccessapachemod-rewrite

I have a simple .htaccess file with the contents below.

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^(.*)$ index.php?s=$1 [L]
</IfModule>

I want to add this rule.

RewriteRule ^p$ index.php?p= 

I tried doing this below but it doesn't work. It seems like both rules are being run. I have tried a couple of different flags and again have had no luck. Could someone tell me how to get this working please.

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^p$ index.php?p= 
     RewriteRule ^(.*)$ index.php?s=$1 [L]
</IfModule>

Best Answer

You must add [L] flag to stop reading rules when one match :

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^p$ index.php?p= [L]
     RewriteRule ^(.*)$ index.php?s=$1 [L]
</IfModule>

Reminder of RewriteRule flags :

  • L = Last. Stop processing RewriteRules once this one matches. Order counts!
  • C = Chain. Continue processing the next RewriteRule. If this rule doesn't match, then the next rule won't be executed. More on this later.
  • E = Set environmental variable. Apache has various environmental variables that can affect web-server behavior.
  • F = Forbidden. Returns a 403-Forbidden error if this rule matches.
  • G = Gone. Returns a 410-Gone error if this rule matches.
  • H = Handler. Forces the request to be handled as if it were the specified MIME-type.
  • N = Next. Forces the rule to start over again and re-match. BE CAREFUL! Loops can result.
  • NC = No case. Allows [jpg] to match both jpg and JPG.
  • NE = No escape. Prevents the rewriting of special characters (. ? # & etc) into their hex-code equivalents.
  • NS = No subrequests. If you're using server-side-includes, this will prevent matches to the included files.
  • P = Proxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-server fetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad.
  • PT = Pass Through. Take into account Alias statements in RewriteRule matching.
  • QSA = QSAppend. When the original string contains a query (http://example.com/thing?asp=foo) append the original query string to the rewritten string. Normally it would be discarded. Important for dynamic content.
  • R = Redirect. Provide an HTTP redirect to the specified URL. Can also provide exact redirect code [R=303]. Very similar to RedirectMatch, which is faster and should be used when possible.
  • S = Skip. Skip this rule.
  • T = Type. Specify the mime-type of the returned content. Very similar to the AddType directive.

Extract from this (very) complete post : https://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as

Related Topic