Folder name clashing with html page (using htaccess) - html

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.

Related

How To make Custom Links Without Showing page not found error [duplicate]

I want to redirect all pages on my site (including index) to UnderWork.html
and I'm doing this using .htaccess with this code:
RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html
...and its works fine.
Now I am adding some more code to my .htaccess to redirect all traffic to the non-www domain and now my code looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html
Here is where I have a problem. If I enter a URL like: domain.in/xyz then it works as before, but if I use a URL like: www.domain.in/xyz then Apache converts it to coincart.inxyz/.
What am I doing wrong here? How can I get what i want? Thanks in advance.
Below are rules that work for me:
RewriteEngine On
# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]
# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]
# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]
To show a custom page when a directory root (domain root in particular) is requested, use the Apache's DirectoryIndex directive:
DirectoryIndex UnderWork.html

Rewrite rules for .htaccess

I have a basic question about the .htaccess file.
Currently mine consists of:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
Redirect all links to the hypertext transfer protocol secure (https) of the website.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
Rewrite all .html files without their file extension.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/?(.+)/?$ /$1/index.html [L]
RewriteRule ^/?(.+/)index\.html$ /$1 [R=301]
Remove the index.html from site URLs. Should I remove the file extension (.html) because of the 2nd rule?
ErrorDocument 404 /404.html
Self-explanatory. Should I remove the file extension here as well?
And overall - is the code correct, is this the way it should look like in my .htaccess file?
I appreciate your help.
I ended up using this code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
ErrorDocument 404 /404.html
The index.html remove from URLs rule was useless, because I changed my links in the html part:
Homepage
to:
Homepage
and analogical:
English Homepage
to:
English Homepage

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.

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]