Zaki Aziz
Zaki Aziz

Reputation: 3852

Using .htaccess to redirect /sitemap.xml to /index.php/sitemap/

I'm using codeigniter to generate a page that contains XML data. The URL of the file is located at /index.php/sitemap, but I want the URL to be accessed via /sitemap.xml

My .htaccess file is already modified to allow users to access URLs without entering /index.php/ before URLs. This is the .htacess file I'm using:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

RewriteRule ^sitemap\.xml$ index.php/sitemap [L]

Everything is working fine, except the redirection of the sitemap.xml file. I'm on a windows machine running XAMPP

What am I doing wrong?

Upvotes: 1

Views: 5690

Answers (3)

stormdrain
stormdrain

Reputation: 7895

RewriteRule ^.*sitemap\.xml$ index.php/sitemap [L]
RewriteRule ^(.*)$ /index.php/$1 [L]

You are matching before you reach the sitemap check so it always redirected to index.php.

This is the example from the user guide:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^.*/(.*)$ /index.php/$1 [L]

You can just shove the check for sitemap in there before the standard index.php rewrite

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^.*/sitemap\.xml$ index.php/sitemap [L]
RewriteRule ^.*/(.*)$ /index.php/$1 [L]

Upvotes: 0

Olaf Dietsche
Olaf Dietsche

Reputation: 74008

The order of the rules is important. The rules are processed in the order they are written. The pattern .* matches everything and therefore the rule

RewriteRule ^.*$ index.php [NC,L]

rewrites all requests before they can reach the last rule.

When you put the sitemap.xml rule at the top, it will work as expected.

Upvotes: 0

Aken Roberts
Aken Roberts

Reputation: 13447

Skip the .htaccess modifications and use a normal route instead.

$route['sitemap\.xml'] = 'sitemap';

Upvotes: 2

Related Questions