regular express problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

regular express problem

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: regular express problem

Post 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]
 
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Re: regular express problem

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regular express problem

Post 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'"
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regular express problem

Post 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.
Last edited by prometheuzz on Wed Jul 30, 2008 8:12 am, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: regular express problem

Post 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 :(
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regular express problem

Post 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.
ody3307
Forum Newbie
Posts: 21
Joined: Wed Jul 30, 2008 7:29 am

Re: regular express problem

Post 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: \.)
Post Reply