Heavy Rains Cause Floods and a Sinkhole in Delhi, India - Jul. 19 / Jul. 20, 2020

Are there any external factors (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 file (it performs redirection but not for all URLs):

# BEGIN WordPress  RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ RewriteRule (.*) http://example.com/$1 [R=301,L,QSA]  

[Edit] I've already made some changes since I first posted the question, changes that didn't bring any improvement with regards to 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  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.*   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'    Header set Cache-Control 'max-age=2592000, public'   Header set Cache-Control 'max-age=2592000, public'   Header set Cache-Control 'max-age=216000, private'   Header set Cache-Control 'max-age=216000, public, must-revalidate'   Header set Cache-Control 'max-age=1, private, must-revalidate'    Header unset ETag  FileETag None  Header unset Last-Modified  RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L] #BEGIN WordPress  RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php  # END WordPress 

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  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  

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] 
  • 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.
  • @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 , 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 - 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 , 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.

radio za vas: Charles Robertson | Želite nas kontaktirati?