WesleyE
WesleyE

Reputation: 1384

mod_rewrite wont take third rule

I've got a problem with my mod_rewrite code. I want the rewrite to happen based on 3 rules:

  1. If it is not a known file (images, xml), reroute to index.php (For Zend MVC)
  2. If there is no www before the domain name, add it
  3. If the requested url is www.domain.com/page/pagename, rewrite to www.domain.com/page/show/id/pagename

So far I've got the first two working, but I can't seem to get the third one working. This is my code:

RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) index.php [QSA,L]

RewriteCond %{HTTP_HOST} ^fietsnl.nl
RewriteRule (.*) http://www.fietsnl.nl [R=301,L]

RewriteCond %{REQUEST_URI} ^page/([a-zA-Z_]+)$
RewriteRule ^page/([a-zA-Z_]+)$ /page/show/id/$1 [L] 

If I request http://www.domain.com/page/pagename, it still rederects to the first rule Can you put me in the right direction on what to change?

Thanks!

Upvotes: 0

Views: 39

Answers (2)

derekaug
derekaug

Reputation: 2145

The L in

RewriteRule (.*) index.php [QSA,L]

Tells mod_rewrite to stop processing rules so the two below that will never get processed in the case that the file doesn't exist on the server.

I'm not a master at mod_rewrite, but by moving the last rule before the first rule it should fix you're problem.

RewriteEngine on

RewriteCond %{REQUEST_URI} ^page/([a-zA-Z_]+)$
RewriteRule ^page/([a-zA-Z_]+)$ /page/show/id/$1 [L] 

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) index.php [QSA,L]

RewriteCond %{HTTP_HOST} ^fietsnl.nl
RewriteRule (.*) http://www.fietsnl.nl [R=301,L]

Upvotes: 1

Ulrich Palha
Ulrich Palha

Reputation: 9529

Rules are matched in the order that they appear.

I assume that page/pagename is not an existing file or directory, so it will match the first rule and no other rules will me processed.

In general, you want to have rules arranged from most specific to least specific, and have redirects typically before rewrites.

A simple fix would be to move the first rule to the end.

Upvotes: 0

Related Questions