Forum Moderators: phranque

Message Too Old, No Replies

Specify Content Type to a Rewrite Rule

         

yaashul

6:15 am on Aug 25, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



How can I specify a content type to a rewrite rule. e.g. I generate Sitemap using this rewrite rule

RewriteRule ^sitemapindex\.xml(\.gz)?$ /sitemap.php?gzip=$1 [QSA,L,NC]


But the content type remain php rather than text/xml

Please suggest!

g1smd

8:17 am on Aug 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the PHP script use the HEADER directive to send the correct HTTP header data.

yaashul

8:47 am on Aug 25, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



There is a problem...if we use php header along with server header (in htaccess) server header always super seed php header hence can't specify it there

phranque

9:40 am on Aug 25, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



But the content type remain php rather than text/xml

".php" is a file type but not a content type.
what is the exact text of the Content-Type header(s) you get with the response?
what is the directive you are using in .htaccess to specify the Content-Type header?

lucy24

9:41 am on Aug 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I generate Sitemap using this rewrite rule

RewriteRule ^sitemapindex\.xml(\.gz)?$ /sitemap.php?gzip=$1 [QSA,L,NC]

But the content type remain php rather than text/xml

?
mod_rewrite is a powerful tool, but there are things even it can't do. It can't change anything about a file-- including its name. All it does is point the user (human or robot) in a different direction. Here you are telling the server that if anyone asks for sitemapindex.xml, send them quietly to sitemap.php instead.

yaashul

10:10 am on Aug 25, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



Lucy,

you are right thats what I want to do... but in .htaccess file I have set
# NEVER CACHE
<FilesMatch "\.(html|htm|cgi|pl|php)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>



For all php script but for sitemap I wud like robots and browser to cache it... So I use


header('Cache-Control: public');

in sitemap.php script. but there is a small issue. Header directives (mod_header) are processed just before the response is sent to the network (and after any content generator like PHP).

So sitemapindex.xml always show this header

Cache-Control: max-age=0, private, no-store, no-cache, must-revalidate

phranque

11:05 am on Aug 25, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



instead of FilesMatch try using the <LocationMatch> Directive:
http://httpd.apache.org/docs/current/mod/core.html#locationmatch
this way the match will occur with the requested URL instead of the resolved filename.

now about the file type/content type questions i asked above...

lucy24

7:34 pm on Aug 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For all php script but for sitemap I wud like robots and browser to cache it

If your only concern is to make search engines treat the sitemap differently, frankly I wouldn't spend any time on it. g### is going to do whatever it likes with the sitemap-- and I doubt that they will go along with pretending a .php file is an .xml.

phranque

12:05 am on Aug 26, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



g### is going to do whatever it likes with the sitemap

more importantly, on the relatively rare occasions that sitemap.xml is requested, why would you care if it is cached?
i would prefer to guarantee that the most recent version is always grabbed.
and even more rare that it would be requested by a "real person" using a browser.

I doubt that they will go along with pretending a .php file is an .xml.

the user agent should never know about the internal rewrite to a php script unless a subsequent iteration exposes it with an external redirect, in which case you should fix THAT problem first!

yaashul

1:33 pm on Aug 26, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks everyone for ur Important suggestions...