Glitchy configuration of .htaccess?

Hi,

I just stumbled upon a strange phenomenon.

I have .htaccess setup to redirect all non www requests to www.

So [noparse]http://example.com[/noparse] is redirected 301 to [noparse]http://www.example.com[/noparse]

But, files in the root category are not rewritten for some reason.

So if I call [noparse]http://example.com/robots.txt[/noparse] it won’t rewrite to [noparse]http://www.example.com/robots.txt[/noparse]

How come?

I use


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

Can someone explain this to me? Why is this happening?

Might be because of the nasty use of (.*) …

Try this


RewriteCond %{HTTP_HOST} ^example\\.com [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [L,R=301]

Hello,

Thanks for the suggestion. Tried it, but http://example.com/robots.txt still loads without www.

:frowning:

Could you post your complete .htaccess please?

Your .htaccess syntax are same as what i’m using but it work for me. I think it’s because of your robots.txt url have been cached. Please delete cache and check again or please send your complete htaccess. :slight_smile:

Hi, here is my .htaccess. It has a seo-friendly url mod from cdseo pro for x-cart.

Also, how would I go about un-caching the robots.txt file? Is there any way to disable its caching?


# ------------------------------------
# CDSEO Pro
# ------------------------------------
# This directory index is recommended to replace the
# x-cart default within this .htaccess file
DirectoryIndex home.php index.html index.php

# Enable mod rewrite engine
RewriteEngine On

Options +FollowSymLinks
Options +Indexes

RewriteCond %{HTTP_HOST} ^www\\.buy-replica\\.([a-z]+[a-z\\.]*)$ [NC]
RewriteRule ^robots\\.txt$ robots_%1.txt [L]

RewriteCond %{HTTP_HOST} ^buy-replica\\.com [NC]
RewriteRule .? http://www.buy-replica.com%{REQUEST_URI} [L,R=301]

RewriteBase /
RewriteRule ^(.*)\\.asp http://www.buy-replica.com/ [R=301,L]
RewriteRule ^(.*)\\.aspx http://www.buy-replica.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^buy-replica\\.com [NC]
RewriteRule ^(.*)$ http://www.buy-replica.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^buy-replica\\.ru [NC]
RewriteRule ^(.*)$ http://www.buy-replica.ru/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^buy-replica\\.de [NC]
RewriteRule ^(.*)$ http://www.buy-replica.de/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^buy-replica\\.fr [NC]
RewriteRule ^(.*)$ http://www.buy-replica.fr/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^buy-replica\\.es [NC]
RewriteRule ^(.*)$ http://www.buy-replica.es/$1 [L,R=301]

# .htpasswd protection
# May be required if your htpasswd files are located below document root (i.e. cpanel)
# Remove # at the beginning of this line if your unable to access password protected folders:
ErrorDocument 401 "Unauthorized"

# Some servers require this Options directive to be disabled
# Remove # at the beginning of this line to disable:
#Options -Multiviews

# Some servers require the Rewritebase directive to be enabled
# Remove # at the beginning of this line to enable:
#RewriteBase /

RewriteCond %{REQUEST_URI} !^/(payment|admin|provider|partner)/
RewriteCond %{REQUEST_FILENAME} !\\.(gif|jpe?g|png|js|css|swf|php|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ cdseopro.php
# ------------------------------------
# / CDSEO Pro
# ------------------------------------




Try this:


# ------------------------------------
# CDSEO Pro
# ------------------------------------
# This directory index is recommended to replace the
# x-cart default within this .htaccess file
DirectoryIndex home.php index.html index.php

# Enable mod rewrite engine
RewriteEngine On

Options +FollowSymLinks +Indexes -MultiViews

RewriteRule \\.aspx?$ http://www.buy-replica.com/ [R=301,L]

RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\\.buy-replica\\.([a-z]+[a-z\\.]*)$ [NC]
RewriteRule ^robots\\.txt$ robots_%1.txt [L]

# .htpasswd protection
# May be required if your htpasswd files are located below document root (i.e. cpanel)
# Remove # at the beginning of this line if your unable to access password protected folders:
ErrorDocument 401 "Unauthorized"

RewriteCond %{REQUEST_URI} !^/(payment|admin|provider|partner)/
RewriteCond %{REQUEST_FILENAME} !\\.(gif|jpe?g|png|js|css|swf|php|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ cdseopro.php
# ------------------------------------
# / CDSEO Pro
# ------------------------------------

I’ve removed RewriteBase / because you usually don’t need it. If this breaks the complete .htaccess – it does on a small percentage of hosts – please put it back.

As for clearing the cache, I know there is a way – using mod_headers I think – but I’m not really sure how.

Thank you very much for optimizing the code! I’m 100% sure it should work, but the same is happening since post #1:frowning: Any suggestions where I should look to find the problem?

Sorry, no idea, it really should work :s
I see however that you aren’t running on Apache but on ngix? Maybe that’s got something to do with it?