Assuming the file that renders your "one-page" site is index.html
and your "pages" are /about
, /contact
and /help
that correspond to elements with id
s of the same name (less the slash prefix).
Your root .htaccess
code would be something like the following, using mod_rewrite:
RewriteEngine On
RewriteRule ^(about|contact|help)$ index.html [L]
The above rewrites requests for the 3 URLs stated to index.html
. So, those 3 URLs get the same HTTP response.
Then, JavaScript on the page would be something like:
// Get the URL-path, less the slash prefix
const id = location.pathname.substring(1);
// Get the HTML "page" element whose ID is passed in the URL.
const element = document.getElementById(id);
if (element) {
element.scrollIntoView();
}
Note that the id
could be empty or index.html
. (Aside: You could redirect from /index.html
to /
in .htaccess
.)
Although, as mentioned in comments, this might have accessibility issues over using the standard fragment identifier. And /#about
and /about
will return the same response - the first being handled by the browser and the second by JavaScript.
Reference:
NE
flag if you are issuing an external redirect to the#fragid
. You could instead treat the one-page website as a front-controller (rewriting the request) and let JS read the URL and do the necessary, as mentioned in my answer. @Wayne