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

Is there any extern factor (that is, not in the .htaccess file) that can cause the RewriteRule not to work for specific URLs (of existing pages)?

I have the following code in the .htaccess (it performs redirection but not for all URLs):

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

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php

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

</IfModule>

[Edit] I've already made some changes since I've first posted the question, changes that didn't bring any improvement in what regards the way redirections are made - so the problem still needs a solution. The whole .htaccess file content is:

Options +FollowSymLinks -MultiViews

RewriteEngine on




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>




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



#BEGIN WordPress
<IfModule mod_rewrite.c>



RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php


</IfModule>


# END WordPress
share|improve this question
    
I don't suppose by any chance this is a website powered by magento is it? I'm banging my head against a table with the very same problem! –  MrG May 14 '11 at 11:16

1 Answer 1

Old Answer

.htaccess rewrites work from the top down. Once a re-write match has been found then apache stops there. So you need to put your re-direction first like this:

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

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

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php

</IfModule>

New Answer

instead of:

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

try:

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

Or if that doesn't work try:

    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain\.com(.+)? [NC]
    RewriteRule (.*) http://domain.com/$1 [R=301,L]
share|improve this answer
    
Thank you for answering, @Emmanuel . As strange as this may sound, doing what you suggested doesn't help me see all the pages on other domains being redirected. Here you can see more about the URLs that are'n directed and other ways I've tried to make this work. After days and days of trying to solve this I tend to believe the error hides somewhere else. –  keiki Apr 13 '11 at 8:51
    
@keikki - Mm... okay, how about trying this for the redirect: RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] and RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] –  Emmanuel Apr 13 '11 at 10:21
    
@Emmanuel , I've tried RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] and RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] and the result was: "The page isn't redirecting properly" - on the one hand; on the other hand, correct me if I'm wrong, but how would additional domains be redirected towards the main one if the ^www\.example\.com isn't negated with "!" ? Yes, it is a good idea to make the URLs not to be case sensitive, but this doesn't address the problem I confront now: domain forwarding with the strange behavior of working only on certain URLs. Thanks again for trying, though! –  keiki Apr 13 '11 at 17:30
    
@keiki - Mmm... okay I think the !^(www\.)? in your rewrite is a conditional for matching a url with or without the www. Could you post some of the urls that aren't being redirected? And is the code you've got above all of your .htaccess file? –  Emmanuel Apr 13 '11 at 21:55
    
@Emmanuel , URLs with or without www are matched by (www\.)?, while the "bang" (!) negates the whole URL, because I wanted every URL with the %{HTTP_HOST} different of "domain", to be redirected. An URL that isn't redirected is www.anotherDomain.com/existentDirectory/. I'll gladly edit my post to add all the .htaccess file. –  keiki Apr 14 '11 at 8:51

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.