Page 1 of 1

regular express problem

Posted: Wed Jul 30, 2008 5:17 am
by itsmani1
Hi

I am having problem in fixing the regular expression.
I want to forward all calls to shop.php but at the same time if[there is call i.e http://www.example.com then it should took it to index.php not the shop.php

here is my current expression, it always throw pages to shop.php even if want to go index.php it will take me to shop.pop

Code: Select all

RewriteEngine on
RewriteRule ^/ index.php
RewriteRule ^([a-zA-Z0-9.]+) shop.php

Re: regular express problem

Posted: Wed Jul 30, 2008 5:42 am
by jaoudestudios
Ofcourse it will, your line 3 says to redirect everything to shop.php
You need to add a NOT to it.
i.e.

Code: Select all

 
// not index.php
[^index.php]
 

Re: regular express problem

Posted: Wed Jul 30, 2008 6:02 am
by itsmani1
I change the code like this and still not working, I think i took it wrong, can you please correct me?

Code: Select all

 
RewriteEngine on
RewriteRule [^index.php]
RewriteRule ^([a-zA-Z0-9.]+) shop.php

Re: regular express problem

Posted: Wed Jul 30, 2008 6:42 am
by prometheuzz
jaoudestudios wrote:Ofcourse it will, your line 3 says to redirect everything to shop.php
No it doesn't. The 3rd line should only match a certain string of size 1 or more.
jaoudestudios wrote:You need to add a NOT to it.
i.e.

Code: Select all

 
// not index.php
[^index.php]
 
No. [^ABC] does not mean "anything except the string 'ABC'". It means: "any character except 'A', 'B' or 'C'"

Re: regular express problem

Posted: Wed Jul 30, 2008 6:44 am
by prometheuzz
itsmani1 wrote:Hi

I am having problem in fixing the regular expression.
I want to forward all calls to shop.php but at the same time if[there is call i.e http://www.example.com then it should took it to index.php not the shop.php

here is my current expression, it always throw pages to shop.php even if want to go index.php it will take me to shop.pop

Code: Select all

RewriteEngine on
RewriteRule ^/ index.php
RewriteRule ^([a-zA-Z0-9.]+) shop.php
I have little knowledge in this mod_rewrite thing, but it seems to work properly on my webserver when I ommit the '.' (dot):

Code: Select all

RewriteEngine on
RewriteRule ^/ index.php
RewriteRule ^([a-zA-Z0-9]+) shop.php
So, perhaps that might give you an angle on solving it.

Good luck.

Re: regular express problem

Posted: Wed Jul 30, 2008 8:04 am
by jaoudestudios
No. [^ABC] does not mean "anything except the string 'ABC'". It means: "any character except 'A', 'B' or 'C'"
Yes you are right, my mistake :(

Re: regular express problem

Posted: Wed Jul 30, 2008 8:14 am
by prometheuzz
jaoudestudios wrote:
No. [^ABC] does not mean "anything except the string 'ABC'". It means: "any character except 'A', 'B' or 'C'"
Yes you are right, my mistake :(
No problem.

Re: regular express problem

Posted: Thu Jul 31, 2008 5:47 pm
by ody3307
Not sure I understand your question.

If you are saying you want all calls to the base domain address (http://www.example.com) to go to index.php and everything else (like: http://www.example.com/someproduct/) to redirect to shop.php then...

You don't need a rule for the base domain calls.

The easiest way to handle the other calls is to make sure all urls end with the trailing slash and test for that.
Like this:

RewriteRule ^/([^/]+)/ shop.php

In other words, give me everything that is not a slash, capture it in the parentheses, followed by a slash.
If you need to you can then pass the captured text on to your program to use as you wish. If you have no need for the captured text, leave the parentheses off, but it hurts nothing to keep them.

So, a call to http://www.example.com/blue-widgets/
Would be redirected to shop.com

If you want to pass the text along with the redirect, do this.
RewriteRule ^/([^/]+)/ shop.php?product=$1

This would redirect to shop.php and pass a $_GET variable called "product" with a value of "blue-widgets".

Also, the dot character is a metacharacter in regex. So if you need to test for a literal dot, you must escape it (like this: \.)