views:

20

answers:

2

I've been trying everything to manage a redirect from www.domain.com to domain.com, but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.

So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

#####################################################################
# Don't show directory listings for URLs which map to a directory.
# For security reasons, Option all cannot be overridden.
# Options -Indexes
#####################################################################

Options ExecCGI  Includes  IncludesNOEXEC  IncludesNOEXEC  MultiViews  SymLinksIfOwnerMatch  Indexes -Indexes

# Set the default handler
DirectoryIndex index.php



#####################################################################
#
#                               REWRITES
#
#####################################################################

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]

#  Redirect all to .php
#  Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]


# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]


<IfModule mod_expires.c>
 <FilesMatch "\.(gif|jpg|jpeg|png|flv|swf|css|js|html?|xml|txt|ico)$">
  ExpiresActive On
  ExpiresDefault "access plus 10 years"
 </FilesMatch>
</IfModule>
<FilesMatch "\.(gif|jpg|jpeg|png|flv|swf|css|js|html?|xml|txt|ico)$">
 FileETag none
</FilesMatch>


<FilesMatch "\.(html|htm|php)$">
 Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>



#####################################################################
#
#          REDIRECTS (301)
#
#####################################################################

Redirect 301 /en/ /
Redirect 301 /en/index.php /
Redirect 301 /en/search.php /
# ... and so on 

Thank you so much! I've already spent so much time trying to figure this out.

A: 

The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.

RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://example.com/$1 [L,R]
radius
I'm afraid this gives me an internal server error (500)
rafleo
+1  A: 

You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.

RewriteEngine On

# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]

# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}      !\.php$
RewriteRule ^.+$ $0.php [L,R=301]

# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
Tim Stone
rafleo
No problem. In case you needed that second part that wasn't working (sorry about that), I went ahead and updated it to take care of the many edge-cases that would also cause that to cause problems (though I'm not fully awake, so there may be more that I failed to consider).
Tim Stone