Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2012, 04:35 AM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
When to close Rules?

I could use some help with my .htaccess file(s).

Someone told me last week that I needed to end my Mod_Rewrite with some closing command/character. I believe it is supposed to be written like this... [L]

That confuses me, because from what I have read, if you have an [L] then everything after that is ignored, however, in the .htaccess I have been using on my live website, I have commands after an [L] and things still work...

Could someone look at what I have and help me clean things up?

Here is a snippet...
Code:
#----------------------------------------------------------
#Eliminate index.php.
RewriteEngine on
RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"
RewriteRule index\.php http://www.mywebsite.com/ [R=301,L]


#----------------------------------------------------------
#Redirect all non-www calls to www.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


#----------------------------------------------------------
# Build Date: 2012-01-03 2:20pm

RewriteEngine on

#PRETTY:		articles/postage-meters-can-save-you-money
#UGLY:			article.php?slug=postage-meters-can-save-you-money

#RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?slug=$1

RewriteRule articles/([a-zA-Z0-9_-]+)$ articles/article.php?slug=$1


#----------------------------------------------------------
#Clean up Dead Links in Google.
Redirect 301 /browse/articles/3 http://www.mysite.com/article_index.php
Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 01-09-2012, 03:23 PM   PM User | #2
leslie.jones
New Coder

 
Join Date: Nov 2011
Posts: 88
Thanks: 4
Thanked 26 Times in 26 Posts
leslie.jones is an unknown quantity at this point
Hi,

The Command Flag 'L' signals last, as you rightly point out, but only 'fires' if the rule was successfully executed.

Hope that helps,

Leslie

EDIT
I meant to add that if the rule fires and the target rewrite page is in the same directory as .htaccess, then it will execute again applying rules to the rewritten URL. This catches a few people out and can jam you into a loop if you are not careful.

A common 'gaff' is often seen when people try to avoid others hot-linking their images, and then redirect them to another image file of the same type as they are trying to block - an example:

Code:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mywebsite.whateve [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ nohotlinking.jpe [L]
Notice that rewrite rule subtly redirects to a jpe file, rather than one of the types it is trying to block (jpeg, jpg, gif, bmp or png). If this was, instead, nohotlinking.jpg Even with 'L', the ruleset would be parsed again after the initial redirect and hit again because the request for nohotlinking.jpg matches the rule the second time round. Bang, you have an infinite loop.

Typically in such cases the browser may cry something like 'this page is redirecting in a way that will never complete', but more commonly you'll get a 500 error.

Last edited by leslie.jones; 01-09-2012 at 03:44 PM..
leslie.jones is offline   Reply With Quote
Old 01-09-2012, 06:10 PM   PM User | #3
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by leslie.jones View Post
Hi,

The Command Flag 'L' signals last, as you rightly point out, but only 'fires' if the rule was successfully executed.

Hope that helps,

Leslie
I don't really follow you.

To me, that implies I just have on [L] at the end of my .htaccess file.

What about the code I listed above?

I have multiple [L]'s there...


Debbie
doubledee is offline   Reply With Quote
Old 01-09-2012, 06:14 PM   PM User | #4
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
If this rule is satisfied

RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"

then the rewrite below it is executed and nothing else in the file is looked at. Without the [L] the rest of the rules would be tested/executed as well which is usually not what one expects or wants to happen.

If the rule above is NOT satisfied then the next rule is tested

RewriteCond %{HTTP_HOST} !^www\. [NC]

Note that you have 2 instances of RewriteEngine on in the file but you only need one FWIW.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-09-2012, 06:30 PM   PM User | #5
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by djm0219 View Post
If this rule is satisfied

RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"

then the rewrite below it is executed and nothing else in the file is looked at. Without the [L] the rest of the rules would be tested/executed as well which is usually not what one expects or wants to happen.

If the rule above is NOT satisfied then the next rule is tested

RewriteCond %{HTTP_HOST} !^www\. [NC]

Note that you have 2 instances of RewriteEngine on in the file but you only need one FWIW.
Ugh!! I'm sorry, but am not following you guys.

Rewind...

In my original post and code, I am trying to do the following things...

1.) #Eliminate index.php.

2.) #Redirect all non-www calls to www.

3.) #Create a Pretty URL/Slug

4.) #Clean up Dead Links in Google.


Based on my interpretation of what has been said, my .htaccess file is never getting past #1 since I my first block of code ends with an [L]...
Code:
#----------------------------------------------------------
#Eliminate index.php.
RewriteEngine on
RewriteCond %{THE_REQUEST} "GET /index.php HTTP/1.1"
RewriteRule index\.php http://www.mywebsite.com/ [R=301,L]

Is that correct?

I doubt that it is correct, because my current production .htaccess has code blocks #1, #2, and #4 and all seem to be working fine all from the same .htaccess file.


Maybe having...
Code:
RewriteEngine on
...causes things to start over again after an [L] ???


Can you guys please help break things down into simplier terms and help me understand this??

Sorry, but Apache/.htaccess/mod_rewrites are NOT my thing!

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 01-09-2012, 07:16 PM   PM User | #6
leslie.jones
New Coder

 
Join Date: Nov 2011
Posts: 88
Thanks: 4
Thanked 26 Times in 26 Posts
leslie.jones is an unknown quantity at this point
I'm pressed for time this evening, so I've not got chance to look deeply in this - but for you're index.php take a look at this:

http://kevinthompson.info/blog/compl...onengine-urls/
leslie.jones is offline   Reply With Quote
Old 01-09-2012, 08:04 PM   PM User | #7
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
Quote:
Originally Posted by doubledee View Post
Based on my interpretation of what has been said, my .htaccess file is never getting past #1 since I my first block of code ends with an [L]...
Incorrect. The RewriteRule line will ONLY be "seen" if the RewriteCond line above it is true.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-09-2012, 08:19 PM   PM User | #8
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by djm0219 View Post
Incorrect. The RewriteRule line will ONLY be "seen" if the RewriteCond line above it is true.
So what happens to what I'm calling #2, #3, and #4??


Debbie
doubledee is offline   Reply With Quote
Old 01-09-2012, 08:34 PM   PM User | #9
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
If the #1 condition is not met then #2 is checked. If it isn't met then #3 and #4 are "hit". Just because a rule is there does not mean it is going to do anything. The rewrite in #3 may result in rule #4 not being used.

You want the [L] on the first 2 rules because you are sending a 301 back to the browser to tell it that the URL it went to is now someplace else. Since you're sending a 301 back it makes no sense to process any more rules. You've already told the browser it needs to go someplace else.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-09-2012, 08:53 PM   PM User | #10
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by djm0219 View Post
If the #1 condition is not met then #2 is checked. If it isn't met then #3 and #4 are "hit". Just because a rule is there does not mean it is going to do anything. The rewrite in #3 may result in rule #4 not being used.
So should I have an [L] after each of the 4 rules?


Quote:
You want the [L] on the first 2 rules because you are sending a 301 back to the browser to tell it that the URL it went to is now someplace else. Since you're sending a 301 back it makes no sense to process any more rules. You've already told the browser it needs to go someplace else.
So if the URL is "www.mysite.com/index.php", then rule #1 fires and removes the "/index.php" and things end, right?


And if any page comes across as "mysite.com", then rule #2 fires and adds a "www" to the domain and things end, right?


And if someone typed in an Article like this...

"www.mysite.com/articles/postage-meters-can-save-you-money"

...then rule #3 fires and changes the URL (behind the scenes) to "www.mysite.com/article.php?slug=postage-meters-can-save-you-money"

...and things end, right? (Or at least they should I guess?!)


And finally, if someone clicked on an old link that went to...
"www.mysite.com/browse/articles/3"

...then rule #4 redirects them to...
"http://www.mysite.com/article_index.php"

...and things end, right? (Or at least they should I guess?!)


So you are saying that these 4 rules are really MUTUALLY EXCLUSIVE, right?

So should I have an [L] after each rule?

Would it matter since I guess each one only fires under different circumstances, although Rules #1 and #2 could happen together quite often.

Am I making sense?

Still a little confused if I am doing things correctly...

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 01-10-2012, 01:01 PM   PM User | #11
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
What you have is just fine. You don't need to stop the rules processing after 3 and 4 because they are not telling the browser to go somewhere else. They are merely changing the resulting URL.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-11-2012, 12:13 AM   PM User | #12
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by djm0219 View Post
What you have is just fine. You don't need to stop the rules processing after 3 and 4 because they are not telling the browser to go somewhere else. They are merely changing the resulting URL.
Okay, glad to hear I got things right.

Thanks!


Debbie
doubledee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:02 AM.


Advertisement
Log in to turn off these ads.