1

I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.

Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.

I use the following rule in my .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]

However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.

Is the redirecting rule wrong? If so, how can I fix it?

I am using Laravel. This is the full .htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On


    RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
    RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]

    # browser request: .php to non-php
    # https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
    RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

    # Now, rewrite any request to the wrong domain to use www.
    # [NC] is a case-insensitive match
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Adam
  • 25,960
  • 22
  • 158
  • 247
  • Ok, does `http://www.domain-a.com/sitemap-a.xml` work from your browser? – anubhava Oct 26 '18 at 09:04
  • @anubhava yes if I try to access the file directly with `http://www.domain-a.com/sitemap-a.xml` then the content of `sitemap-a.xml` is shown as expected. – Adam Oct 26 '18 at 09:06

1 Answers1

1

Can you try this rule just above # Handle Authorization Header line:

RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I tried your snipped and the original above `# Handle Authorization Header` but there was no difference. I also purged my cache. – Adam Oct 26 '18 at 09:35
  • 1
    Change it to `RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC,R]` see if URL in browser changes or not – anubhava Oct 26 '18 at 09:48
  • Still no redirect. I open the page and alwas press `strg` + `f5` to purge cache and refresh. I also tried to remove the `sitemap.xml` file but this gives a `404` error. – Adam Oct 26 '18 at 10:01
  • I un-commented `RewriteCond` so that I only have `RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]` but still no redirect. I also tried only `RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]` but still no redirect. – Adam Oct 26 '18 at 10:08
  • Alright, I tried this and it also didnt work. I now removed my `.htaccess` and the website worked fine (which should not be the case..). I think my laravel app is ignoring the `.htaccess` file. I will try to find the problem here. Thanks for helping me to get here. – Adam Oct 26 '18 at 10:15
  • 1
    I found this https://stackoverflow.com/a/48867772/2311074 I was here 10 month ago. Forgot about it.. Sorry – Adam Oct 26 '18 at 10:17
  • oh didn't know you are on Nginx – anubhava Oct 26 '18 at 10:17