0

I have learnt and written some .htaccess rules and some are executing perfectly. But there are few that are not getting executed and shown error or 404

These are the rules

RewriteEngine on

# index.php?store=xyz (executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteRule /?index.php$ /%1 [END,R=301]

RewriteRule ^/?([a-zA-Z0-9]+)$ index.php?store=$1 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products$ index.php?store=$1&view=products [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]

# index.php?store=xyz&view=products(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteRule /?index.php$ /%1/products [END,R=301]

# index.php?store=xyz&view=products&category=123(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteCond %{QUERY_STRING} category=([^&]+)&?
RewriteRule /?index.php$ /%1/products/%3 [END,R=301]

# index.php?store=xyz&view=sales (error 404)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=sales&?
RewriteRule /?index.php$ /%1/sales [END,R=301]

# index.php?store=xyz&view=sales&sale=123 (error 404)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=sales&?
RewriteCond %{QUERY_STRING} sale=([^&]+)&?
RewriteRule /?index.php$ /%1/sales/%3 [END,R=301]

# index.php?store=xyz&view=single&product=123(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]

Can you please tell me what I may be doing wrong?

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31

1 Answers1

0

You redirect the client from

index.php?store=xyz&view=single&product=123

to

/%1/single/%3

and you have a corresponding RewriteRule

RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]

You also redirect the client from

index.php?store=xyz&view=sales&sale=123

to

/%1/sales/%3

but there is no corresponding RewriteRule, only two

RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END]

So maybe changing one of the "product" rules to "sales" fixes your immediate problem.


Although, you should be aware that the redirecting rules don't do, what you might think.

RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]

Having three RewriteCond, don't correspond to %1 and %3 in your rewrite rule, only %1 is valid, see RewriteRule for an explanation

In addition to plain text, the Substitution string can include

  1. ...

  2. back-references (%N) to the last matched RewriteCond pattern

To have both %1 and %3, you must capture three parts in the last RewriteCond, e.g.

RewriteCond %{QUERY_STRING} store=([^&]+)&view=(single)&product=([^&]+)
RewriteRule ...

See another answer to RewriteCond to match query string parameters in any order for a solution to capture multiple parts.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198