4

I have a part of domain example.com/~mypart. In that folder is index.html. If I visit example.com/~mypart/something I get a "Not Found" error.

I want every address with some string after example.com/~mypart/ to rewrite to example.com/~mypart (index.html) and keep the string so I can use JavaScript in index.html to get that string.

  • I changed "redirect" to "rewrite". Redirects change the URL but rewrites don't. You say you want to give access to the URL to JavaScript, so you will need a rewrite that doesn't change the URL. – Stephen Ostermiller Jun 21 '18 at 13:58
2

This is a standard front-controller pattern. You can use mod_dir's FallbackResource directive in the .htaccess file at /~mypart/.htaccess. For example:

FallbackResource /~mypart/index.html

(If /~mypart is an Apache per-user web directory then just use /index.html.)

Many examples will use mod_rewrite, but unless you have more complex URL rewriting requirements, then the above is sufficient (and recommended).

Reference:
https://httpd.apache.org/docs/2.4/mod/mod_dir.html#fallbackresource

  • 1
    Oh I was looking for answers for about an hour and I did not find anything that used FallbackResource. Very nice! – Reenergy Jun 21 '18 at 17:02
  • Glad you got it working. Just curious, is /~mypart a physical subdirectory on your filesystem, or an "Apache per-user web directory" (as mentioned above)? – MrWhite Jun 21 '18 at 17:30
  • 1
    Apache per-user web directory – Reenergy Jun 21 '18 at 18:07
0

Add the following to your httaccess file:

    # if folder does not exist
    RewriteCond %{REQUEST_FILENAME} !-d
    # if file does not exist
    RewriteCond %{REQUEST_FILENAME} !-f
    # redirect anything to
    RewriteRule ^(.*)$ ~mypart/$1 [L]

credit where is due

And after that, once the page loads, you can use the following javascript code to get the requested file

    var requestedResource = window.location.href.replace('example.com/~mypart/','');
  • Thank you, however it doesn't work for me. Probably I am doing something wrong. But it's okay because solution from MrWhite did work for me and you showed me side of htaccess which I did not know about. So thank you. – Reenergy Jun 21 '18 at 16:58
  • 1
    There are a few issues with these directvies... 1. If using mod_rewrite, you need to enable the rewrite engine, ie. RewriteEngine On. 2. In order to only target the /~mypart subdirectory, these directives would need to go in the /~mypart/.htaccess file (as opposed to the document root), although /~mypart does "look-like" an Apache web-user directory, so this may not be necessary. 3. Either way, you would need to also remove the ~mypart/ from the RewriteRule substitution. 4. This currently rewrites to itself ($1 backreference), this should be index.html. – MrWhite Jun 21 '18 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.