I want all urls to be redirected, except my sitemap xml file in the root directory. The htaccess should allow https://old-domain/xml.xml to resolve with HTTP 200, but it is still redirecting to the new domain at the moment. How can I exclude the file (xml.xml) from the redirect?
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/xml\.xml$
RewriteCond %{HTTP_HOST} ^old-domain\.de$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.de$
RewriteRule (.*)$ https://new-domain.de/$1 [R=301,L]
</IfModule>
I would actually expect this to result in a rewrite-loop (500 Internal Server Error response), due to the slash prefix on the substitution string (denoting a URL-path).
You are likely seeing a cached redirect resulting from the previously implemented 301 permanent redirect. 301s are cached persistently by the browser. (Which is why you should always test with 302 (temporary) redirects.)
Instead of rewriting to the same URL, you should simply not rewrite the request at all. This is achieved by a using a single
-
(hyphen) in the substitution string.Also, I assume you are not including a trailing slash on the request, so the
/?
in the regex is superflous. (The default handler for.xml
files does not permit path-info anyway, so a trailing slash would likely result in a 404 - unless it is rewritten.)The
NC
flag should be unnecessary here - you should be consistently using the canonical URL (which I assume is all lowercase).So, the above rule should be written like this instead:
(Remember to backslash-escape literal dots in the regex.)
This will prevent any further mod_rewrite directives being processed when requesting
/xml.xml
.Your stray closing
</IfModule>
tag implies you have other directives? Needless to say, these redirects need to go near the top of your.htaccess
file.And clear your browser (and any intermediary) caches before testing. Test with the browser inspector open and "Disable cache" checked on the Network tab.
Alternative
Since you only want to exclude one URL/file from being redirected, you could incorporate this as part of your existing redirect, as an additional exception.
For example:
The
!
prefix negates the regex. So it is successful when the requested URL does not match the regex^/xml\.xml$
.OR, avoiding the need for the additional condition: