How to remove .html in htaccess - html

I have a problem removing the .html on the end of a page.
The code I use works perfectly accept on one page /contact.html.
If I rename the html file to anything else then /contact.html it works.
For an example /nothing.html.
If I add an empty html file on my server called contact.html it doesn't work either.
This is the code I use:
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.bijleshelmond.nl} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.bijleshelmond.nl/$1 [R=301,L]

You probably have a folder (or even a file) named contact, you will need to remove that folder (or file first).
The condition is not met in this situation is one of the too:
# Condition broken if the directory 'contact' exists
RewriteCond %{REQUEST_fileNAME} !-d
# Condition broken if the file 'contact' exists
RewriteCond %{REQUEST_fileNAME} !-f

Related

Redirect deleted images url to home page in .htaccess file

I have deleted some images from my website but for SEO purposes I want to redirect those image path(URL) to my homepage. I have put this code in my .htaccess file but it is not working.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^.*\.(.jpg|.jpeg|.png|.gif)$ https://www.homepage.com/ [L,R=301]
Your regex pattern has extra dots.
You may try this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpe?g|png|gif)$ / [L,R=301,NC]

How to remove .html from URL and redirect when the html will be accessed directly

I know that I can remove the .html using the remove html .htaccess code which is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
I still don't like this cause when visitors access my site with a .html extension then the .html will still not be removed and can still be shown by the visitors.
But I want a user to be redirected without a .html extension when he/she access the file directly. For example when a visitor wants to access file1.html directly on the browser then it automatically redirects or delete the .html extension, so instead of showing file1.html when accessed directly on browser then it will automatically rewrite "file1.html" to "file1" without the html extension anymore.
Also additional, how to add a trailing slash?
(Truly sorry if I haven't elaborate clearly.)
In your .htaccess file try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
should remove .html and add a trailing /
source

Folder name clashing with html page (using htaccess)

I have a static html website and I'm using .htaccess to clean up the url by removing the .html from the address.
The issue is when I visit "example.com/path" it clashes with the folder "path" but when I choose to manually enter example.com/path.html I will get the page loading just fine.
The URLs inside the "path" folder are working fine as well with "example.com/path/page1" both with and without .html file extension.
My only alternative just now is to change the folder name from the file name as subtle as possible until I find a solution.
I've had a look at related questions being asked as well as a bit of research on conditions to put into my .htaccess file but having no luck.
Appreciate any help.
EDIT ***
htaccess file ..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk$1 [L,R=301]
Just returning a 403.
Kindly try below code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
Note : Don't forget to replace RewriteBase. As of now i have added / bca my project is in root folder. You can simply add folder name like below
RewriteBase /yourfoldername/
Hope it will work for you.

HTML - Rename index.html for every folder

So I am fairly new to html and web development. I am currently playing around with a small website and wondered if it is possible to rename the 'index.html' file in every subfolder.
I have read a little about the htaccess file but could not find anything specific to my problem.
I want to be able to have something like www.example.com/test with test.html in the /test folder acting as the index.html and the same for www.example.com/apple with apple.html and so on, without having to have 100 index files in total as that seems pretty confusing.
Is this possible with htaccessand how if it is? If not is it possible at all?
You can use:
RewriteEngine on
# remove html (to avoid duplicate content)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite html only if file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.]+)/?$ $1.html [L]
In your .htaccess you do like this
RewriteRule ^index.*$ index.php [NC]
So in other words you replace index.php with what you want to be changed and index with the name that users you want to see.You can also do this with arguments like
RewriteRule ^welcome.*$ index.php?page=welcome [NC]
Try this rule i am assuming files and folder's are present in manner as you have said.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ $1/$1.html [L]

Need to remove .html file extension and duplicate names

I have a .htaccess file with the contents below, that removes the .html file extension for all of my website's pages.
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]
My links now look like www.james-lee.io/resume/resume when before they looked like www.james-lee.io/resume/resume.html. I would like to remove the folder name so the name of the folder is not duplicated by the name of the file minus the .html and the final result looks like www.james-lee.io/resume.
I have seen similar questions but not exactly what I am looking for.
So I have done this task!
Try this code:
RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2 -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/(.*)$ /$1/$1 [END]
Now I try to explain this rules.
first line: you do request like /folder/file
second line: check if /folder/ real existing folder
third line: check if /folder/file is real existing file
fourth line: I use notation %1::%2 because backreferences can only
be used in the left part of RewriteCond. But it possible to reuse
left part in pattern of the right part. So, in the "^(.*)::\1$" I
check all before ::. Then I have result at the \1 backreference. So,
if folder is equal to file, text after :: will be equal to %2.
Next I just redirect to the result (/folder or /file, doesn't
matter, because both are equal)
But if folder == file, redirect will be always to the directory.
So, next I check, if redirect result is existing dir and change the link.
Request example:
http://yourdomain/test/test
(this will be redirected to http://yourdomain/test, but will reference to original link)
I hope, I explain clearly, but if you have any questions, I would glad to answer.
Thank you for insteresting task!
P.S. see also %N backreference inside RewriteCond
UPDATED. Your htaccess have to be like below:
RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2\.html -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/(.*)$ /$1/$1.html [END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]