Regular expression help

I am facing problem in regular expressing, please help me.

My website has two files, index.php, myshop.php when user access mywebsite.com it should take them to index.php and when they try mywebsite.com/anyshop it should take them to that shop.

Here is what i did for this but it did not work.

RewriteEngine on
RewriteBase /
RewriteRule ^/?(index\\.php)?$ index.php [L]
RewriteRule ^/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&order=$3&page=$4
RewriteRule ^/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)$ shop.php?shop=$1&catid=$2&order=$3
RewriteRule ^/([a-zA-Z0-9]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&order=$2&page=$3
RewriteRule ^/([a-zA-Z0-9]+)/([a-z]+)$ shop.php?shop=$1&order=$2
RewriteRule ^/([a-zA-Z0-9.]+)/([0-9]+)$ shop.php?shop=$1&page=$2
RewriteRule ^/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&page=$3
RewriteRule ^/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)$ shop.php?shop=$1&catid=$2
RewriteRule ^/([a-zA-Z0-9.]+) shop.php?shop=$1
RewriteRule ^item/([0-9]+)$ item_detail.php?id=$1

If i make a folder/directory named shopping and try this, it works fine but don’t want to keep shopping there, i want it to work without shopping. like myshop.com/anything

RewriteRule ^shopping/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&order=$3&page=$4
RewriteRule ^shopping/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)$ shop.php?shop=$1&catid=$2&order=$3
RewriteRule ^shopping/([a-zA-Z0-9]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&order=$2&page=$3
RewriteRule ^shopping/([a-zA-Z0-9]+)/([a-z]+)$ shop.php?shop=$1&order=$2
RewriteRule ^shopping/([a-zA-Z0-9.]+)/([0-9]+)$ shop.php?shop=$1&page=$2
RewriteRule ^shopping/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&page=$3
RewriteRule ^shopping/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)$ shop.php?shop=$1&catid=$2
RewriteRule ^shopping/([a-zA-Z0-9.]+) shop.php?shop=$1
RewriteRule ^item/([0-9]+)$ item_detail.php?id=$1

Hi dizyn,

I’m confused.
First off, on which Apache version are you? The rule starting with ^item suggests you’re on 2.x, the rules starting with ^/([a-zA-Z0-9]+) suggest you’re on Apache 1.x and the rule ^/?(index\\.php)?$ suggests you don’t know (:
Figure that out and change all the rules accordingly.

Secondly, RewriteBase is only needed to undo any effects from Redirect or RedirectMatch (mod_alias). Since you don’t have any of those you can safely remove RewriteBase /.

Third, it’s rather odd to redirect index.php to index.php (since nothing happens), and to redirect / to index.php (that’s DirectoryIndex’s job, not mod_rewrite’s). I would personally drop that complete rule since it doesn’t make a lot of sense.

Now, to get to your original question, it looks to me you just need to add another layer for the shop name, i.e.


RewriteRule ^[color=red][a-zA-Z0-9]+[/color]/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&order=$3&page=$4

And of course do that for the other rules as well.

PS. Why do most rules have ([a-zA-Z0-9]+) and two of them have an extra dot in there ([a-zA-Z0-9.]+) ?

I am on 2.X

I used this rule but it does not provide me the values of query string. it just provides me file name “shop.php” it should also return me the query string values

for example when i try to open

example.com/myshop/12/price/2

it should return me values of all 4 variables and it’s not doing that. it’s just returning me shop.php

RewriteRule ^[a-zA-Z0-9]+/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)/([0-9]+)$ shop.php?shop=$1&catid=$2&order=$3&page=$4

thanks for the help

regards,

dizyn

Nope, indeed that won’t match. Let’s take a look at the URL:
/myshop/12/price/2

That has 4 parts.

Now let’s look at the rule:
[a-zA-Z0-9]+/([a-zA-Z0-9]+)/([0-9-a-z-A-Z]+)/([a-z]+)/([0-9]+)

That one has 5 parts.

Since the number of parts is not equal and there are no optional atoms in the regular expression this will never ever match.

First [a-zA-Z0-9]+ will match “myshop”, then ([a-zA-Z0-9]+) will match “12” (although ([0-9]+) would have been better here), then ([0-9-a-z-A-Z]+) matches “price” and then it stops because ([a-z]+) doesn’t match “2”.

It seems like somehow you wanted to have 5 parts in the URL and now you’re only supplying four?