0

I have a sitemap in PHP sitemap-latest.php and i want to change the extension to .xml without affecting the file and content in it.

RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitemap-latest.xml$ sitemap-latest.php [L] #NoEffect
RewriteRule ^sitemap-latest\.php$ sitemap-latest.xml [R=301,L] #Redirect to folders https://www.example.com/var/www/vhosts/mirrorblogs.com/website1/sitemap-latest.xml
RewriteRule ^sitemap-latest.php$ sitemap-latest.xml [L] #404 error for both php and xml

But nothing is happening. example.com/sitemap-latest.php not changing to example.com/sitemap-latest.xml

Mehul Kumar
  • 461
  • 8
  • does this answer your question https://stackoverflow.com/questions/7897353/how-to-change-the-php-file-extension-using-htaccess-file-on-godaddy-linux-hosti ? – dev_mustafa Feb 10 '23 at 05:57
  • @dev_mustafa it is similar things. he replacing all link and i replacing only one file. – Mehul Kumar Feb 10 '23 at 06:32
  • The rules look OKish, though certainly details could be altered. But if you say "nothing is happening" (whatever that means _exactly_ ) then maybe the rules are not getting applied at all. That would be the case if 1. the rewriting module is not present at all (not laoded/activated/not an apache http server) or of 2. you placed the rules in the wrong location or 3. you forgot to enable the interpretation of such rules by means of the `AllowOverride` directive. – arkascha Feb 10 '23 at 07:16
  • 1
    _"example.com/sitemap-latest.php not changing to example.com/sitemap-latest.xml"_ - that is not what that RewriteRule does, it does the exact _opposite_ - when the _incoming_ request is for `/sitemap-latest.xml`, then it will internally rewrite that to `sitemap-latest.php` – CBroe Feb 10 '23 at 07:17
  • If you have links pointing to `example.com/sitemap-latest.php` somewhere that you can't change, or you want to redirect clients that request that "old" URL for some reason, _to_ the `.xml` version - then you need a rule that performs an external redirect first. – CBroe Feb 10 '23 at 07:18
  • @arkascha other rules are in my `.htaccess` is working fine – Mehul Kumar Feb 10 '23 at 08:32
  • @CBroe you mean `RewriteRule ^sitemap-latest.php$ sitemap-latest.xml [L]` like this?. I also tried this. same nothing happening. – Mehul Kumar Feb 10 '23 at 08:33
  • Did you put that rule directly under those two existing RewriteConds? (Meaning, is it the _first_ rule that follows those?) If so, then of course nothing will happen in this particular situation - those conditions prevent the rule from applying, when the requested URL directly matches an existing file or folder. And since you _have_ a `sitemap-latest.php` that actually exists here, no rewriting would be done. – CBroe Feb 10 '23 at 08:58
  • Plus for an external redirect, you'd also need the `R` flag. – CBroe Feb 10 '23 at 08:59
  • @CBroe `RewriteRule ^sitemap-latest.php$ sitemap-latest.xml [R,L]` along with two existing RewriteConds. Nothing happened – Mehul Kumar Feb 10 '23 at 09:17
  • Well what did I just explain about those RewriteConds? – CBroe Feb 10 '23 at 09:18
  • `RewriteRule ^sitemap-latest\.php$ sitemap-latest.xml [R=301,L]` alone this redirect to `https://www.example.com/var/www/vhosts/mirrorblogs.com/website1/sitemap-latest.xml` instead of `example.com/sitemap-latest.xml` – Mehul Kumar Feb 10 '23 at 09:20
  • Please share more details. What's the expected behaviour? "not changing to" - what does that mean? A RewriteRule is usually not changing any URL, but solely how the server processes it. Also, please remove the irrelevant tags - or explain how your problem is related to cPanel and Plesk – Nico Haase Feb 13 '23 at 16:00

0 Answers0