Forum Moderators: phranque

Message Too Old, No Replies

htaccess - can't locate the error

I am having trouble redirecting the homepage of my site

         

markds

9:32 am on Jul 10, 2012 (gmt 0)

10+ Year Member



Hi,

I cannot seem to get my .htaccess file to work (I am very new to this). I have a redirect from all pages from no-www to www, but it simply does not work for the homepage ("www.example.com/" and "example.com/") where the two versions are duplicate.

Furthermore I have removed trailing slashes from all URL's except the homepage and blog homepage (www.example.com/ and www.example.com/blog/).

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{REQUEST_URI} !^.*admin.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^.*admin.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

How do I maintain the rules above, but redirect "example.com/" to "www.example.com/" ?

Thanks a lot for your help.

Best regards
Mark

[edited by: incrediBILL at 10:02 pm (utc) on Jul 10, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

9:41 am on Jul 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As it stands, your code is almost unreadable.

Add a blank line after each RewriteRule for clarity.

Use example.com in this forum and the [ code ] ... [ /code ] tags.


You have four rules.

The first rule should swap with the second rule (keeping the conditions with the right rule).
That is, the non-www/www rule should be the last of the redirects.

Follow the two redirects with your two rewrites.

Your main problem is that
RewriteCond %{REQUEST_URI} !^/$ [NC]

explicitly stops the redirect for root requests.

markds

10:02 am on Jul 10, 2012 (gmt 0)

10+ Year Member



Hi again,

Thanks a lot for the fast answer. I have tried to swap the first and the second rule. I am a bit unsure of what you mean by "follow the two redirects with your two rewrites". After swapping the two first rules, I get a "too many redirects" error.

This is the current code where I get that error. Can you locate the error? Once again thank you so much. I really appreciate you helping out a newbie!


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{REQUEST_URI} !^.*admin.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]

RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

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

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]

g1smd

2:01 pm on Jul 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Scrap that last code completely.

Conditions that pair with a rule are those that are stated before the rule.

You've moved the rules around and not kept the right conditions with each one.



In your original code you have 4 rulesets.

Each ruleset has some conditions and a rule.
Place a blank line after each RewriteRule.

The first two rulesets are redirects.

The last two rulesets are rewrites.

The order of the two redirects is wrong. They need to swap places.

markds

4:48 pm on Jul 10, 2012 (gmt 0)

10+ Year Member



Once again thank you very much. I have now done those changes, but it still does not seem to work.. Could there be anything else? It works for all other pages, except for the homepage.



RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^.*admin.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]

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

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]

aakk9999

9:28 pm on Jul 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{REQUEST_URI} !^/$ [NC]

As g1smd said, this condition says:
"if the request is not for the domain root"

This means that the redirect will only happen if the request is NOT for the domain root - and hence your non-www to www does not work for domain root.

Try to remove this condition line from the second ruleset.

g1smd

10:05 pm on Jul 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The new code is a lot better.

The very first condition in the very first ruleset is redundant.

!^.*admin.*$
is very inefficient.
Using
!/admin/
should be good enough (no anchoring).

^.*$
simplifies to
.*


Your main problem is that
RewriteCond %{REQUEST_URI} !^/$ [NC]

explicitly stops the redirect for root requests.

markds

9:14 am on Jul 11, 2012 (gmt 0)

10+ Year Member



Hi aakk9999 and g1smd,

The redirects are working perfectly now! Thank you so much for your help. I really appreciate it.

Have a great day!

Mark

g1smd

2:02 pm on Jul 11, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We could have just fixed the one thing you asked about, but you'd have been back within weeks with far bigger problems after deploying the code.

Prefer to fix all issues.