A really weird .htaccess issue - html

So let us say that my website is www.example.com and I have a contact.html page in the root. The URL normally shown in the address bar is: http://example.com/contact.html
For my site, I want the URL to have:
no www
no .html
a trailing slash
So the final result should look like this: http://example.com/contact/
Condition No. 1 is default, so that doesn't bother me.
For Condition 2 (no '.html'), I have the following code into my .htaccess file place in the root directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
...and it works perfectly.
Now coming on to Condition 3 (enforcing trailing slash), I have the following code into the .htaccess file before the 'Condition 2' rewrite commands:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
...this does not work; The page redirects to the Error 404 page and the address bar shows: http:/example.com/404.html... YES, with the trailing slash :/
What am I doing wrong?
I have a folder called archives in the root directory of which I want indexing to be done. So I simply added a .htaccess file in the 'archives' folder and added the following, simple code:
Options +Indexing
But it doesn't work; I get redirected to the Error 403 page! I'm 99% sure that it is due the .htaccess file in the root directory.

Try with:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.+) %{REQUEST_URI}/ [R=301,L]
# remove html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^\.]+)/?$ $1.html [NC,L]

Related

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.

from .html to no .html angular

so basically I've this htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#RewriteRule ^(.*) /index.html [NC,L]
RewriteRule ^ index.html [L]
</IfModule>
but old website has all the .html url's like www.mysite.com/contact.html
thing is it's first page in a lot of google searches and i don't want to loose all that indexed pages, so i what i want is to get that and remove the .html
also, i've custom url's like www.mysite.com/product/whatever_my_name.html
same story, grab that and make it www.mysite.com/product/whatever_my_name
thanks in advance
RewriteCond %{REQUEST_URI} ^((?:/[^/]+)+).html$
RewriteRule ^ %1 [NS,L,R=301]
Testing only the client's request is important to avoid interference with serving any other html docs, e.g. your main index.html.

Removing html file extensions from website urls

Im editing my .htaccess file in the root folder of my website server to get rid of all the .html extensions in the URL's (4 in total) and I save it and its not working. Obviously something I'm doing wrong...
This is what i have in .htaccess:
# Begin Muse Generated redirects
# End Muse Generated redirects
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Use this if you must use .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

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]

Getting a 404 with htaccess

I am hoping somebody can help me with this.
I have a htaccess file in my public html folder for my website with the following:
RewriteEngine on
RewriteRule (.+)\.html$ /$1 [L,R=301]
So when I visit my site to newsletter.html it redirects to domain.co.uk/newsletter instead of domain.co.uk/newsletter.html which is what I want..
but i also get a 404 saying the page cant be displayed. The webpage in question is named newletter.html in my file manager.
Does anyone know why it cant be displayed?
To hide .html extension use this code in root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]