Home > database >  Is it possible to configure apache2 to accept query strings appended at the end of a url when separa
Is it possible to configure apache2 to accept query strings appended at the end of a url when separa

Time:03-24

Example... https://myisp.com/mypage.html/foobar I would like to be able to have some js running on mypage.html that can read the foobar as if it were a query parameter. I understand how to write the necessary js, but am trying to understand if Apache can be configured to serve up the html page and pass the final term to a script running on the page.

CodePudding user response:

There's nothing you can do on the server (ie. Apache) to fool client-side JavaScript in to seeing a "query string" when there is no query string in the visible URL that the browser sees.

Using Apache mod_rewrite you can internally rewrite the request to convert /foobar into a query string - but that's internal to the server. The browser/JavaScript does not see that.

You could implement an external redirect and visibly convert the URL from /mypage.html/foobar to /mypage.html?foobar (or /mypage.html?/foobar) - but I expect that is not what you require.

However, you don't need to convert this to a query string for JavaScript to be able to read it...

/mypage.html/foobar

The part that starts with a slash after a valid filename (eg. /foobar in this example) is called additional pathname information (aka "path-info"). Normally, Apache rejects such requests on the default file handler for text/html files, so the above request would normally result in a 404 Not Found response.

However, you can allow path-info on all URLs by including the following directive at the top of the root .htaccess file:

AcceptPathInfo On

Apache will now serve /mypage.html instead of generating a 404. The browser sees the full URL, ie. /mypage.html/foobar and this is available to JavaScript in the pathname property of the location object (ie. window.location.pathname) which you can then parse to extract /foobar (or foobar).

For example:

// Request "/mypage.html/foobar"
let pathInfo = /\.html\/(.*)/.exec(window.location.pathname)[1];
console.log(pathInfo); // foobar

pathInfo is then your "query string".

  • Related