To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 03-21-2010, 05:18 PM   #1
bookbuyer2000
Member
 
Join Date: Jul 2007
Posts: 46
Mod_Rewrite 301 question

Hi, I know this is a PHP forum, but some of the smartest coding people visit here... so hopefully you won't mind helping me with a mod_rewrite question.

To redirect visitors from example.com to WWW. example.com, I've seen 2 variations:

1.)
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

2.)
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

The 2nd variation has an extra ^ and an extra $

I don't know what those mean, but both variations seem to work on my server -- I'm sure they are different enough that one is probably "better" than the other... Which one??

Thank you!!
bookbuyer2000 is offline   Reply With Quote
Old 03-21-2010, 05:28 PM   #2
halojoy
phpMaster
 
Join Date: Jun 2005
Location: Europe
Posts: 2,991
^ symbol for absolut beginning of the text
(.*) any number of any character
$ symbol representing the absolute end of the text

in this case I dont think the two makes any difference
but other cases sure is a difference using
^ for beginning

say you want to find a string that begins with abc
^abc

only search 'abc' would give hits even inside the string, not only in beginning
__________________
--
Wish you all a Good 2010

Last edited by halojoy; 03-21-2010 at 05:31 PM.
halojoy is offline   Reply With Quote
Old 03-21-2010, 05:36 PM   #3
bookbuyer2000
Member
 
Join Date: Jul 2007
Posts: 46
Quote:
Originally Posted by halojoy View Post
^ symbol for absolut beginning of the text
(.*) any number of any character
$ symbol representing the absolute end of the text
Thank you for explaining that! However, looking at the example using the ^ and the $, I can't figure out what it's doing there before the "http" part.
Since it is before the "http" part, does that mean if someone types a bunch of stuff *before* the http in the browser? I've never heard of that. I wonder what an example would be of that.
bookbuyer2000 is offline   Reply With Quote
Old 03-21-2010, 05:41 PM   #4
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 4,408
^ = Start of line anchor
$ = End of line anchor


in some case it's clear with out them in other cases without them the rewrite will fail, so its best to use them.

$index.html^

is only going to match index.html

where as

$index.html

would also match

index.html-and-anythinjg-after-it

since .* in your cases matches everything anyway there's no obvious value to them


Module mod_rewrite URL Rewriting Engine
dagon is offline   Reply With Quote
Old 03-21-2010, 05:45 PM   #5
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 4,408
Quote:
Originally Posted by bookbuyer2000 View Post
Thank you for explaining that! However, looking at the example using the ^ and the $, I can't figure out what it's doing there before the "http" part.
Since it is before the "http" part, does that mean if someone types a bunch of stuff *before* the http in the browser? I've never heard of that. I wonder what an example would be of that.
The rule has 2 parts Pattern and Substitution

in your case pattern =

^(.*)$

Substitution=

http://www.example.com/$1

you are replacing anything with http://www.example.com/*What you match*
dagon is offline   Reply With Quote
Old 03-21-2010, 06:03 PM   #6
bookbuyer2000
Member
 
Join Date: Jul 2007
Posts: 46
Quote:
Originally Posted by dagon View Post
The rule has 2 parts Pattern and Substitution

in your case pattern =

^(.*)$

Substitution=

http://www.example.com/$1
I think I understand. the ^(.*)$ part does seem redundant though -- I just can't see what circumstance that would ever be necessary. If you have an example, it would help me learn (if convenient for you to provide example).

May I also ask: what exactly does the "/$1" part do? I thought $1 is just a backreference... but I can't figure out what that's backreferencing? Does it mean that if the website visitor types in variables, that should get appended, too? For example: http://example.com/index.html?item=red
bookbuyer2000 is offline   Reply With Quote
Old 03-21-2010, 06:57 PM   #7
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 4,408
$1 is what ever the (.*) matches

if you had (foo)(bar)

you can use $1 =foo, $2=bar
dagon is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:55 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.