.htaccess issue when including custom 404 error page - apache

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.

Have it this way in your site root .htaccess:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteRule ^/?([a-z]+)/?$ /includes/$1.html [L,NC]

Related

.htaccess RewriteRule not working in different host

My Current .htaccess rule's example
RewriteRule ^([^/\.]+)-([0-9]+) my_article.php?article_permalink=$1&article_id=$2 [NC,L]
ErrorDocument 404 /404errorpage.php
Output:
https://example.com/my-article-example-1
In my localhost and current host these rules working fine. But in other host
10 day ago when I had changed my hosting the first rules was not working. it's showing 404 page (returns 404).
Today in new host the second rule ErrorDocument not working. It's showing
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Expected output:
for any 404 error show 404errorpage page
Now I can't understand what is the problem.
With your shown samples, please try following htaccess Rules. Please make sure your htaccess rules file, my_articile.php file both are in same folder(root folder). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^(.*)-(\d+)/?$ /my_article.php?article_permalink=$1&article_id=$2 [QSA,NC,L]
ErrorDocument 404 /404errorpage.php

Trail slash is causing 404 error and improper url behavior: APACHE/ PHP

Hoping one of you can help. I am developing using php, without a framework, therefore all of my routing rules are in .htaccess as opposed to .php
So here is my problem. Say my website is http://example.com and my navigation is pretty straight forward, meaning if you click the 'press' page link you will go to http://example.com/press or http://example.com/press/ which are both fine.
However, if you do happen to go to http://example.com/press/ click another internal link in my nav, such as 'about' etc.. I will not take you to '/example' but rather '/press/example' which shows a 404 since there is not page or directory to handle this.
I tried 'DirectorySlash Off' in my .htaccess but all that did was prevent any of these pages from showing if a url has / at the end.
I have a very basic .htaccess setup, See below:
<IfModule mod_rewrite.c>
RewriteEngine On
# Options -Indexes
# GEN CONFIG
# Handle Errors
ErrorDocument 403 /views/errors/404.php
ErrorDocument 404 /views/errors/404.php
ErrorDocument 303 /views/errors/404.php
# Url renamin
RewriteRule ^(about-us|About-Us|ABOUT-US)/?$ views/about-us.php [L]
RewriteRule ^(press|Press|PRESS)/?$ views/press.php [L]
RewriteRule ^(faq|Faq|FAQ)/?$ views/faq.php [L]
</IfModule>
go to http://example.com/press/ click another internal link in my nav, such as about etc.. I will not take you to /about but rather /press/about
This problem is not due to rewrite rules but due to your use of relative paths.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Apache - Use a unique index.php with existing folders

I want to redirect all the pages of my website (non existing files and folders) to a unique index.php. It's simple, this is my .htaccess into the root of my website:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
ErrorDocument 400 /index.php?er=400
ErrorDocument 401 /index.php?er=401
ErrorDocument 403 /index.php?er=403
ErrorDocument 404 /index.php?er=404
ErrorDocument 406 /index.php?er=406
ErrorDocument 408 /index.php?er=408
ErrorDocument 411 /index.php?er=411
ErrorDocument 500 /index.php?er=500
It works fine for non existing files and folders BUT I need to keep some real folders to store files and I get a 403 Forbiden error when I call "site.com/folder/"
For example if folderA doesn't exists, every call to "site.com/folderA/..." works and I have a 200 OK response for the folder, subfolders and files.
But if folderB exists, I get 404 for all non-existing files and 403 for non-existing subfolders instead of the redirection to my main index.php.
What solutions Do I have to keep thoses folders and redirect all non existing urls to my index ?
Thank you
If you do not only want all non-existing folders to be routed to your index.php (which your rewrite rules will achieve), but the existing ones as well, instead of getting a 403 for those, you can simply use the DirectoryIndex directive like this:
DirectoryIndex /index.php
DirectoryIndex does not only accept a file name, but a local URL as well – and the leading slash makes it relative to the domain root.
So for all your existing folders, your index.php located at the domain root will now be served as the index document.
This could of course be achieved by other means as well – but using mod_rewrite for this would involve more checks, and using ErrorDocument 403 would still write an entry into the server’s error log for every existing folder you request.

Domain repeated twice in 404 error page when using htaccess

I am developing a .htaccess file for my site to clean up the urls. I am trying to rename my contact page to make sure its working but I keep getting a 404 error page saying that my url missing. The .htaccess files appears to be working fine but my domain is being repeated in the 404 error page. Here is my .htaccess file so far.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for contact.php
RewriteRule ^message$ contact.php [NC,L]
Here is the 404 error
The requested URL /domain.com/domain.com/contact.php was not found
on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
My .htaccess file is in the root of my site. It is probably a simple problem but I have been trying all the solutions I could find but to no avail.
What happens is, you are using relative links when rewriting URL. This causes the server to look for /domain.com/domain.com/contact.php.
Use absolute URL in rewrite:
RewriteRule ^message$ /contact.php [NC,L]

htaccess giving error - 404

i have found a solution here, which I am looking for
Creating dynamic URLs in htaccess
so basically I modified the given htaccess code (following code is from solution)
RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1
I changed it to fit my needs to
RewriteRule ^blog/([A-Za-z0-9-])/?$ /blog.php?id=$1
but when I visit the url localhost/blog/1 it gives me 404 Error, that's the only line in my .htaccess file, what I am doing wrong?
You say that's the only line in your .htaccess, don't you have this one too (just before yours) ?
RewriteEngine On
you .htaccess should contain at least :
RewriteEngine On
RewriteRule ^blog/([A-Za-z0-9-])/?$ /blog.php?id=$1