1

This is my first time using .htaccess and currently my .htaccess file is redirecting to my custom 404 page. This is being handled with the following line in my .htaccess file:

ErrorDocument 404 /404.html

On top of this, I need to rewrite a few urls so that /addition/ points to /includes/addition.html (this is one example). So I add the following:

RewriteEngine On
RewriteRule /addition/ /includes/addition.html

But this then serves me a 500 error when Is hold be getting 404 error. On top of this, when I point to mysite.com/addition/ the browser isn't fetching addition.html from my includes folder.

Would someone please explain to me how to have these two rules working without effecting the other, and correct my secondary rewrite rule?

Danke.

3
  • 1
    try to put first the ErrorDocument rule, and then all other rules Commented Jan 3, 2018 at 10:35
  • @anubhava: Admittedly, it should be in my includes folder. But it is currently in my site's root directory. So I don't believe this will be causing it. Commented Jan 3, 2018 at 10:38
  • @anubhava: Yes, it loads the correct file. I have reordered my rules as per @Nicholas Nur's suggestion and now the custom 404 loads correctly if I go to a directory that does not exist. However, if I punch in http://example.com/addition/ I am getting my custom 404 as well, rather than /includes/addition.html being fetched. Any help is much appreciated. Commented Jan 3, 2018 at 10:46

1 Answer 1

1

Have it this way in your site root .htaccess:

ErrorDocument 404 /404.html

RewriteEngine On
RewriteRule ^/?([a-z]+)/?$ /includes/$1.html [L,NC]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This has solves the custom 404 showing up when I type in http://example.com/addition/. The only other thing is addition is one of a few urls that need rewriting. The urls only include [a-z]. How would the RewriteRule be written to handle any lowercase alpha characters, then find the corresponding file in the /includes/ folder? e.g. subtraction.html --> /subtraction/ as well.
Thank you V much. This works perfect. I've also updated my 404 rule and included my custom error page in my includes folder. Thanks for picking that up, too. Marked as correct answer and upvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.