.htaccess file to remove .html from url not working - html

I'm trying to get rid of the .html in the url of pages, and I found that code in the .htaccess file is the only way doing so and I've tried nearly every method and code online but it just wouldn't work.
Made an .htaccess file and put it in the public_html file. The general code I'm using is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
But I've tried many many many other variations of the code so I think the issue isn't with the code but the way the file is getting loaded in to the website?
And yes, all my hrefs are shortened without the .html: ex/ <li>Contact</li>
The main error is in that I always get a 404 error - not found for when I try to go to a page, ex/ website.com/contact. But website.com/contact.html works fine or cannot get / error
I've tried this on both my hosted website and testing through vscode live server and I feel like it's an issue not with the .htaccess file somehow? Since this solution works for nearly everyone but me.

#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
is what worked for me, from plothost , you seem to be missing a line.

Related

Remove .html extension in url

Today I tried to remove html file extension from my website, for example:
example.com/page.html to example.com/page
I watched some tutorials, but nothing seems to work...
I created .htaccess file in root directory
Copied code (also tried different ones):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
It didn't work when I opened my website as file, with Live Server (VS Code extension) or actual website (hosted on Replit)
Any idea, why it doesn't work? Any help appreciated...
See whole repository
Edit: Someone said, I have to remove .html file extension. I get error that the file is not found
Yo should do it in this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC, L]
SOLVED
As someone said, .htaccess file doesn't work on Replit. I've done following:
Made folder for every .html file
Moved that file inside of the folder I made
Renamed the file to index.html

Subdirectory is being accessed instead of a html file of the same name

So, I've just got my .htaccess file working and it removes the .html extensions just fine. However, I now have a problem where a url pointing to my work page www.mywebsite.com/work is now targeting the subdirectory work which contains all the specific project files that can be accessed via the work.html page. I made the subdirectory because I want the url string to read as www.mywebsite.com/work/project-name whilst also having a clean and organised root folder instead of filling it up with 100+ html files.
I've searched for a solution all day but I feel like my limited knowledge is causing me to overlook the answer, so I apologise if this is a dupe. I thought I'd ask specifically on here as a last resort. Would love any suggestions from you, and if this already has a clear answer, please mark this as a dupe and point me in the right direction!
Incase it might help, the contents of my .htaccess file are as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]*
I finally found a good solution to this that also follows the fix admitted by Amit.
Firstly, add DirectorySlash Off
then I removed RewriteCond %{REQUEST_FILENAME} !-d
and finally I removed the "!" from "!-f". Overall, the .htaccess should look like this:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Solution was found in this post.

Need to redirect missing URLs to new host

I'm moving a blog from one site to another and repurposing the original site. I want to maintain all existing links that point to the site and hopefully maintain SEO page ranking.
Old URL: http://www.companyabc.com/2010/04/test.html
New URL: http://blog.companyabc.com/2010/04/test.html
The way I'd like to do it is to use a custom 404 error page on www.companyabc.com like this:
<html><meta http-equiv="REFRESH" content="0;url=http://blog.companyabc.com/%1"></html>
where %1 is the original URI (/2010/04/test.html), but I don't know if that's possible.
Another option is to use an .htaccess file that redirects if the URL is not found, but I haven't gotten that to work either. I'm sure I'm doing something wrong in the rewrite condition:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.*)\.html$ https://blog.companyabc.com/$1.html [R=301,L]
Any suggestions? Thanks for the help.
I got it working using the following .htaccess configuration:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://blog.companyabc.com/$1 [R=301,L]
I never tried the REQUEST_FILENAME value because I mistakenly thought it only applied to files that can be downloaded from the website. I didn't realize it also applies to the .html files in the blog.
With this solution all URLs that don't exist at www.companyabc.com will be redirected to blog.companyabc.com instead of showing a 404 error page, which is what I'm looking for.

index.html is not loading as default home page

I've offered to help a friend by hosting their website (using my account with iPage), however there's an issue I can't resolve. The homepage, FirstnameSurname.com, returns a 404 error.
The namepointers are working correctly as I can access www.FirstnameSurname.com/index.html, however when trying to load www.FirstnameSurname.com it returns the 404.
I've tried changing the folder name (on the server) from FSurname to FirstnameSurname so it matches the domain but it fixes nothing.
Poking around in the htcaccess file I've not been able to fix it either, even when adding DirectoryIndex index.html.
What is going on? How can I fix this problem? Any help greatly appreciated!
This might work on htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]

webpage returning 403 error and not showing the actual page

I have a page on my website ( it's html ) which is named "providers.html".
in order to remove the .html part from url, I added this code to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
now the problem is that I have a folder which is named "providers" and contains files related to that page.
now when I go to the url, I get a 403 error because browser loads the folder and not the html page.
I really appreciate if any one can give me some guidance to solve this problem.
thanks
This might be what you're looking for:
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ([^\.]+) $1.html [NC,L]
Turned off directory slashes. This part is the most important part. Basically, if a directory exists, it shouldn't add a slash. This way, we can continue checking rules against exact URI input.
Added a check to see if the the current request does not end with a slash.
If it doesn't, rewrite to the respective HTML file
Otherwise, load up the contents of the directory. You'll more than likely want to have another .htaccess file in there to prevent listings etc., and only allow access to existing files.
Update: Removed ^ and $ from the rule, as I don't believe it is necessary here.