RewriteRule not working for certain URLs

Posted by keiki on Pro Webmasters See other posts from Pro Webmasters or by keiki
Published on 2011-04-11T10:35:46Z Indexed on 2011/11/13 10:11 UTC
Read the original article Hit count: 318

Filed under:
|

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.

© Pro Webmasters or respective owner

Related posts about domains

Related posts about htaccess