Mobile app version of vmapp.org
Login or Join
Mendez628

: Htaccess sitemap.xml rewrite to /sitemaps/$_SERVER["MYVAR"]/sitemap.xml I am trying to update htaccess to write as following: htaccess sitemap.xml rewrite to /sitemaps/$_SERVER["MYVAR"]/sitemap.xml This

@Mendez628

Posted in: #Apache #Htaccess

I am trying to update htaccess to write as following:

htaccess sitemap.xml rewrite to /sitemaps/$_SERVER["MYVAR"]/sitemap.xml

This rewrite should only occur if sitemap.xml is requested in root (otherwise the above would loop)

I came up with this

RewriteCond %{REQUEST_URI} !^/sitemap.xml
RewriteRule .* /sitemaps/$_SERVER["MYVAR"]/sitemap.xml [L,NC]


But this is not working, server error. Anyone know how to do this?



And on a side note: rewrite to /sitemaps/domain/sitemap.xml would be fine too
(where domain is captured from the request host either domain.com or domain.com)

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Mendez628

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

You can't use a PHP superglobal eg. $_SERVER["MYVAR"] in an htaccess expression. (Apache .htaccess files are processed before PHP gets a go, but anyway $_SERVER["MYVAR"] is PHP syntax not Apache mod_rewrite syntax). However, depending on what MYVAR contains, there might be an alternative.


This rewrite should only occur if sitemap.xml is requested


Your current RewriteCond directive actually does the complete opposite and results in a rewrite when "sitemap.xml" is not requested. The ! prefix negates the regex.


rewrite to /sitemaps/domain/sitemap.xml would be fine too (where domain is captured from the request host either domain.com or domain.com)


This is possible, something like the following:

RewriteCond %(HTTP_HOST) (www.)?([a-z0-9-]+).com
RewriteRule ^sitemap.xml /sitemaps/%2/sitemap.xml [L,NC]


Where %2 is the 2nd parenthesised sub pattern in the RewriteCond directive. However, before this you should already have your canonical www or non-www redirect, so you should already know whether HTTP_HOST contains the www subdomain or not - this will simplify your RewriteCond pattern.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme