Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess - Rewrite all files to index.php except sitemap.xml

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]
like image 667
khaled Avatar asked Oct 19 '25 22:10

khaled


1 Answers

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]
like image 89
tobiv Avatar answered Oct 22 '25 12:10

tobiv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!