Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

There are a few domains pointing towards the same server, and of course I need them all redirect to only one of them.

Redirects work, but only for certain URLs.

What works: http://www.domain.com, http://domain.com, domain.com/index.html, domain.com/index.php, domain.com/nonExistentDirectory, and if I click in the menu the following URLs are also redirected correctly: domain.com/foo/bar, domain.com/foo/bar.html or .php or other extension.

What doesn't work: domain.com/existentDirectory, domain.com/foo/bar (if I type the URL in the address bar).

If anyone will have the time and skill and will to tell me where's the mistake, I'll be deeply grateful.

Here's my .htaccess file:

AddHandler x-httpd-php .html .htm

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 2592000 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>

<ifModule mod_headers.c>
  <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch "\\.(css)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch "\\.(js)$">
    Header set Cache-Control "max-age=216000, private"
  </filesMatch>
  <filesMatch "\\.(xml|txt)$">
    Header set Cache-Control "max-age=216000, public, must-revalidate"
  </filesMatch>
  <filesMatch "\\.(html|htm|php)$">
    Header set Cache-Control "max-age=1, private, must-revalidate"
  </filesMatch>
</ifModule>

<ifModule mod_headers.c>
  Header unset ETag
</ifModule>
FileETag None

<ifModule mod_headers.c>
  Header unset Last-Modified
</ifModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>


# END WordPress


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


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


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


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


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

Thinking that the above version was overkill, I've also tried to redirect all the requests for domains different than the main on to be redirected to it like this:

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

Is it also wrong? Because it doesn't work either!

P.S. @Sodved I've tried that and it doesn't help (I comment here because I can't seem to be able to comment your answer.) Removing the following piece of code didn't solve the issue either, so the problem must be somewhere else:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>


# END WordPress

New details: using this tool for checking the redirects I got the following results for the URLs that are not redirected:

Checked link: http://domain.com/aDirectory/

Type of link: direct link

(note the trailing slash above) and:

Checked link: http://domain.com/aDirectory

Type of redirect: 301 Moved Permanently

Redirected to: http://domain.com/aDirectory/ (no trailing slash here)

I hope/suspect I'm getting closer to the cause of this behavior.

share|improve this question

migrated from serverfault.com Apr 11 '11 at 10:44

This question came from our site for professional system and network administrators.

2 Answers 2

I think you need to move the domain name rewrites to before the # BEGIN Wordpress section. This should do the trick as it means all the domain name checks are done before it even cares about the pathnames.

I can only assume the index.php RewriteRule is firing, and then the [L] flag means its the end of the rewrite stuff, thus your domain name stuff does not get triggerred.

share|improve this answer
    
If I do that, @Sodved , all I get is that URLs containing a wrong location are no longer redirected, and I get a blank page instead; on the other hand, the URLs that weren't redirected before the change are not redirected after the change, either. So this doesn't seem to be a solution. –  keiki Apr 15 '11 at 16:44
    
Sorry about that. Guess I'd have to set it up for myself to know for sure. Don't have teh time/resources at the moment. Sorry. –  Sodved Apr 16 '11 at 13:18
    
Nothing to be sorry for, @Sodved , on the contrary, thank you so much for trying to help! In the mean time I got a bit closer to the cause of this behaviour, I'll also post here all the details when my research will be ready. –  keiki Apr 16 '11 at 17:06
    
@keiki: Try moving the RewriteEngine On and RewriteBase / lines to the top of the file too. They should appear before any RewriteRule or RewriteCond lines. –  Ilmari Karonen May 12 '12 at 12:13

I have a number of domains redirecting to a canonical one for the church website I run. It does use virtual hosts (my hosting company's control panel sets that all up); in the DocumentRoot of the virtual host for the non-canonical names I have an .htaccess consisting of

Redirect permanent / http://www.canonicaldomain.com/

Given that you say in a comment to another answer that you don't have access to httpd.conf, I presume that you're with a hosting company. Do they have a control panel to set up domains/subdomains with a different root?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.