user892134
user892134

Reputation: 3214

Mod rewrite 404 error

The below mod rewrite doesn't work.. For example with a url like http://www.mywebsite.com/category/general .. I get a 404 error.

RewriteEngine On
RewriteBase /
RewriteRule ^category/([A-Za-z0-9-]+)&type=([A-Za-z0-9-]+)&r=([A-Za-z0-9-]+)&g=([A-Za-z0-9-]+)&page=([A-Za-z0-9-]+)/?$ /category.php?c=$1&type=$2&r=$3&g=$4&page=$5 [L]

If i use the mod rewrite below it works, shouldn't there be a better way rather than having to type out all the combinations.

RewriteRule ^category/([A-Za-z0-9-]+)/?$ /category.php?c=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&type=([A-Za-z0-9-]+)/?$ /category.php?c=$1&type=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&page=([A-Za-z0-9-]+)/?$ /category.php?c=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&type=([A-Za-z0-9-]+)&page=([A-Za-z0-9-]+)/?$ /category.php?c=$1&type=$2&page=$3 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&r=([A-Za-z0-9-]+)/?$ /category.php?c=$1&r=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&type=([A-Za-z0-9-]+)&r=([A-Za-z0-9-]+)/?$ /category.php?c=$1&type$2&r=$3 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&r=([A-Za-z0-9-]+)&page=([A-Za-z0-9-]+)/?$ /category.php?c=$1&r=$2&page=$3 [L]
RewriteRule ^category/([A-Za-z0-9-]+)&type=([A-Za-z0-9-]+)&r=([A-Za-z0-9-]+)&g=([A-Za-z0-9-]+)&page=([A-Za-z0-9-]+)/?$ /category.php?c=$1&type=$2&r=$3&g=$4&page=$5 [L]

Upvotes: 1

Views: 448

Answers (1)

IBBoard
IBBoard

Reputation: 969

Your first rule set requires every argument to exist, so it is never triggering on your example URL. The second rule set appears to be working on your example because your first rule handles the "just has a word after 'category/'" situation.

If you want a single rule, try:

RewriteEngine On
RewriteBase /
RewriteRule ^category/([A-Za-z0-9-]+)(&type=([A-Za-z0-9-]+))?(&r=([A-Za-z0-9-]+))?(&g=([A-Za-z0-9-]+))?(&page=([A-Za-z0-9-]+))?/?$ /category.php?c=$1&type=$3&r=$5&g=$7&page=$9 [L]

Or, since the arguments all appear to map directly:

RewriteEngine On
RewriteBase /
RewriteRule ^category/([A-Za-z0-9-]+)(&type=[A-Za-z0-9-]+)?(&r=[A-Za-z0-9-]+)?(&g=[A-Za-z0-9-]+)?(&page=[A-Za-z0-9-]+)?/?$ /category.php?c=$1&type=$2&r=$3&g=$4&page=$5 [L]

Both of those assume that a) you'll always have the first bit after "category/" and b) the remaining options can be mixed and matched but will always be in the same order. The second one is better because it means you can extend it in future if you want - the first one has run out of back-references (it uses $9).

Upvotes: 1

Related Questions