I am trying to redirect every php file to index.php except sitemap.xml. The problem is that sitemap.xml is redirected also to index.php. How to redirect all php files to index.php and sitemap.xml to sitemap.php ?
RewriteEngine On RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\ [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^/?(.*)\.php$ index.php [L,QSA] RewriteRule sitemap.xml sitemap/sitemap.php [L]
You need an additional RewriteCond to exclude sitemap.php, otherwise when sitemap.php is requested it will fulfill the two conditions !-d
and -f
and will again be rewritten to index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\ [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/(sitemap.php)/
RewriteRule ^/?(.*)\.php$ index.php [L,QSA]
RewriteRule sitemap.xml sitemap/sitemap.php [L]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With