Home > Mobile >  .htaccess Rewrite Rule for (s)html File, Get Parameter lost
.htaccess Rewrite Rule for (s)html File, Get Parameter lost

Time:09-28

since a few hours I'm trying to create a rewrite rule, where instead of a php file a rewrite to an shtml file should be the result. The filename is rewritten, but I always lose the get parameter (Is not passed to the shtml file when called). If I do the same with a php file it works.

Does not work: RewriteRule ^search/(.*)$ LookAt.shtml?q=$1 [L,NC]

Does work: RewriteRule ^search/(.*)$ LookAt.php?q=$1 [L,NC]

I already tried QSA etc. somehow nothing works (Google returns many results for .php, for .(s)html I didn't find anything useful), I hope someone can give me a tip, I'm already going crazy with this problem....
Thanks!

CodePudding user response:

I use : console.log(window.location.search); (To debug the issue)

That's the problem. You can't use client-side JavaScript to read the query string from a URL that is rewritten server-side. Given a request for /search/foo there is no query string on the client-side request.

Client-side JavaScript only sees the requested URL in the browser. For example:

// Outputs "https://example.com/search/foo"
console.log(location.href);

The same applies to the .php file. But you were perhaps checking $_GET['q'] or $_SERVER['QUERY_STRING'] instead in the server-side script?

Since you were using .shtml I assumed you would be using Server-Side-Includes (SSI) to access the query string. For example:

// Outputs "q=foo"
console.log('<!--#echo var="QUERY_STRING" -->');

If you are only using client-side JS then you'd probably just parse the requested URL directly in order to extract the search phrase. eg. /search/foo. (No need to rewrite the request to append a query string in this case. Simply rewriting to LookAt.shtml would be sufficient.)

  • Related