|Conditional ErrorDocuments with rewritten urls|
I would like to set up multiple .htaccess 404 ErrorDocuments based on language for a site that uses mod_rewrite.
Foreign language pages are requested as www.example.com/de/page.htm which is rewritten to www.example.com/index.php?lang=de&page=page
I tried putting an .htaccess in an empty de directory with a German ErrorDocument but that was just ignored.
I also tried, after some research the <location> directive with both the requested and rewritten urls but both caused an internal server error - and I'm not even sure if that belongs in .htaccess.
I'm no .htaccess expert and I am stumped. Is there any way I can acheive what I want?
You can't use <Location> envelopes with htaccess. At all, ever. So that's out. Contrary to the name, "Location" means URL, not physical location. Physical location is "Directory" --which you also can't do in htaccess, because htaccess itself is directory-specific.
Having RewriteRules in more than one htaccess file is VERY perilous and needs to be undertaken with extreme caution. You won't blow up the server or crash the site; you're just liable to get unintended results. Or, more likely, liable to not get the intended results.
But before you even start looking at the rules: Where will the 404 be coming from? Are they ordinary server 404s, or do they arise after the fact from your php?
If you're looking at language-specific error pages, it may actually be a lot easier if the 404 is issued by php rather than by the server. Then all you have to do is include a language-appropriate error document at the point where you currently include a generic error document.
I think the answer to your question is that 404 pages are issued by php - with a 404 header function. Then a custom error page is served according to the ErrorDocument in htaccess.
To go about it in php, can I make the 404 page a php file which I could them use alternative language text based on parsed request?
Thanks for your help.
What I said above is incorrect. Looking into this, I discovered (much to my surprise despite the fact that I wrote the code!) that error pages are served both by the server and php. Php for requests that enter the mod-rewrite process; the server for requests that dont.
And following your hint to use php, I have managed to sort this out using a dynamic 404 page. It wasnt even hard.
A lesson in not plugging away at things late at night.
Thanks for pointing me in the right direction.
Whoops! We overlapped each other. Oh well.
|can I make the 404 page a php file which I could them use alternative language text based on parsed request? |
Yes, exactly. If the 404 is generated by the php, the ErrorDocument directive never gets involved anyway, because the server doesn't know anything about it. As soon as the server sends the request to the php page, its job is done; as far as the server is concerned, everything is a 200 and it can go home :)
So your php page already contains-- or should contain!-- a few lines that "Include" the complete 404 document. (If it doesn't, users will be met with a perfectly blank page. This is either funny or alarming, depending on whether the user knows what's happening.)
Now, since the php has been told what language it's in, it's trivial to let it choose among a set of different 404 documents to send out. Structurally it's just drawing another page. But do make sure the 404 header is getting sent out too. Humans won't know the difference, but search engines will.
I assume the php has a default language, so if this information is lost or damaged it will still build a page. Your users probably don't want to see a 404 page in Esperanto ;)
What you have suggested is pretty much what I did. Php sets the 404 header and includes the 404 page. The 404 page (which I changed from htm to php) decodes the query string and includes content depending on a lang variable.
I preferred to set the language of the 404 based on which part of the site is being browsed. That just makes more sense to me. But it also means that the 404 simply defaults to english if there is no lang var in the querystring - which includes when the server instead of php serves the 404.
Anyway, thanks again for your help.