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
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:
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..
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
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]...
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!
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
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?
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
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.