2

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>
Till Noah
  • 21
  • 2

1 Answers1

0
RewriteRule ^xml.xml/?$ /xml.xml [NC,L]

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:

RewriteRule ^xml\.xml$ - [L]

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

RewriteCond %{REQUEST_URI} !^/xml\.xml$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.de [NC]
RewriteRule (.*) https://new-domain.de/$1 [R=301,L]

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:

RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.de [NC]
RewriteRule !^xml\.xml$ https://new-domain.de%{REQUEST_URI} [R=301,L]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • I modified the code in the question for the "alternative" option you gave. Can I add it like that? Regarding your question about other directices in the htacess. They are automated w3 cache code from the plug in. – Till Noah Apr 26 '21 at 19:13
  • @TillNoah Yes, you can have it the way you have written it in the question. I was simply combining the two conditions that check the `HTTP_HOST` variable into a single condition. The `$` at the end of the `RewriteRule` _pattern_ is superfluous. However, as noted above, you would seem to have a caching issue that is resulting in the redirect you are seeing (either that, or something else entirely is triggering the redirect)? – MrWhite Apr 26 '21 at 19:26
  • yes, i also think its a caching issue. any idea how i can resolve this? I'm already using the chrome dev network advice, which you gave me. I dont have any other code in place which would trigger a redirect... – Till Noah Apr 26 '21 at 19:31
  • i definitely cleared my browser cache and it still redirects nonetheless. Is this really a caching issue? – Till Noah Apr 26 '21 at 19:47
  • Well, if it's not a caching issue then (as mentioned above) "something else" is redirecting you and the redirect directives here are not actually doing anything? Look at the HTTP response headers of the redirect - what do they tell you? (Add these to your question.) Are there any intermediary proxy servers that could be caching the response? What happens if you remove these directives? – MrWhite Apr 26 '21 at 21:45
  • i think i got it work now: ( loca-dating.de/xml.xml. ) the problem was that the sitemap was placed in a subfolder instead of the main directory, since its the primary domain. – Till Noah Apr 27 '21 at 07:44