0

I'm very new to web development, and know nothing of back-end dev. I have a website hosted by a 3rd party on a Linux server, using cPanel.

I have a index.html page that is loaded when you go to the website www.domain.com and I have a second file page2.html that loads when I go to www.domain.com/page2.html

Problem 1:

I want to be able to go to www.domain.com/page2 and have page2.html load.

Problem 2:

When I go to www.domain.com/page3.html (which doesn't exist) or www.domain.com/page3 or any other non-existent url index.html loads instead of giving me a 404 error. I have made a custom 404 page (404.shtml) which is in the same directory as index.html and page2.html (/public_html/).

How do I get this 404 page to show when I go to a non-existent URL? I checked the error log on cPanel and it shows a File does not exist error.

[Fri Aug 29 20:16:48 2014] [error] [client ...*] File does not exist: /home/adminName/public_html/page3, referer: http://domain.com/page3/

Any pointers in the right direction would be very helpful.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • The URL stuff looks like a duplicate of [How can I generate a url with the username?](http://stackoverflow.com/questions/22210915/how-can-i-generate-a-url-with-the-username), and here's some links for [custom 404 pages](https://stackoverflow.com/search?q=apache+custom+404). It's best to ask questions one at a time here. – halfer Aug 29 '14 at 22:18

2 Answers2

1

To remove the suffix (.html) from your url, you would need to use .htaccess.

The following will first check if the request is a file (images/css etc.) or directory, then serve the file without the suffix.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

The following will serve a custom 404 error document

ErrorDocument 404 /404.shtml
  • The 404 page now works, but not the url rewrite. When I try to go to `domain.com/page2` I get a 404. I've been looking through the Apache [mod_rewrite documentation](http://httpd.apache.org/docs/current/mod/mod_rewrite.html), but haven't been able to find the problem. – SaulTheBear Aug 29 '14 at 22:31
  • Just found a solution that did work by using your code as a search query. I'm not entirely sure why the one worked and the other didn't. I posted it as an answer below. Thanks for your help! – SaulTheBear Aug 29 '14 at 23:06
0

I just found a similar StackOverflow Question for Problem 1, and the second answer, by Łukasz Habrzyk, worked for me.

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Community
  • 1
  • 1