Search code examples
.htaccessencodingurlencodehttp-status-code-500

.htaccess Header command produces error 500 when using escaped URLs


I have lots of web pages which seperately exist as PDF files. I was asked to tell Google via .htaccess what the original HTML version URL of the PDF files is.

All web pages are available via an encoded URL using PHP's urlencode() function. The URLs contain company names.


Working example, company name "Very good company":

<Files company123.pdf>
    Header append Link '<https://www.example.com/company/123/Very+good+company>; rel="canonical"'
</Files>

Once the company name contains a character that needs to be encoded (e.g. German umlauts), the web server delivers an error 500 for the whole directory:

Non-working example, company name "Very bäd cömpäny":

<Files company456.pdf>
    Header append Link '<https://www.example.com/company/456/Very+b%C3%A4d+c%C3%B6mp%C3%A4ny>; rel="canonical"'
</Files>

What do I have to change to solve this problem? Is it wrong that the file contains encoded URLs? Do I have to define unencoded URLs like https://www.example.com/company/123/Very good company and https://www.example.com/company/123/Very bäd cömpäny instead?


Solution

  • https://httpd.apache.org/docs/2.4/mod/mod_headers.html#header

    value may be a character string, a string containing mod_headers specific format specifiers (and character literals), or an ap_expr expression prefixed with expr=

    The percent character is part of this format specifier syntax, so you need to escape it:

    The following format specifiers are supported in value:  
    Format   Description  
    %%       The percent sign  
    …        …
    

    You will need to double all the % characters here.