user1960856
user1960856

Reputation: 31

Two rules in one .htaccess file not working

Below is my code for .htaccess

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /products/product-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /buy/buy-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2

First rule is working fine but its not taking the second rule...Unable to understand what is happening here...

Original URL's are like this
www.example.com/products/product-full-view.php?src=somevalue&id=somevalue

and for second one
www.example.com/buy/buy-full-view.php?src=somevalue&id=somevalue

Please help me to run second rule also.

Thanks in advance

Upvotes: 1

Views: 49

Answers (2)

Denis de Bernardy
Denis de Bernardy

Reputation: 78443

You're trying to match the same pattern, give or take an optional trailing slash, four times. By the time you reach the last two rules, your URL is already rewritten to something else.

You probably want something that looks more like:

RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ /products/product-full-view.php?id=$1 [L]
RewriteRule ^buy/([0-9]+)/?$      /buy/buy-full-view.php?id=$1          [L]

Or:

RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2 [L]
RewriteRule ^buy/([a-zA-Z0-9_-]+)/([0-9]+)/?$      /buy/buy-full-view.php?src=$1&id=$2 [L]

Or something to that order anyway.

Note the stub and the [L] each time: the products/ and buy/ avoid that the same URL refers to two different locations, and the [L] (for "Last") tells the rewrite engine to stop processing rules when it gets matched.

Upvotes: 1

Oswald
Oswald

Reputation: 31647

If a URL matches the second rule, it also matches the first rule. After applying the first rule, the resulting URL no longer matches the second rule. That's why the second rule is never applied.

Upvotes: 0

Related Questions