Correct way to remove folder name from website URL - html

There are a couple of posts that cover the question of removing folder paths from URLs, but none of the answers explain how to link the page using html after adding the .htaccess code.
The folder structure looks like this:
root
.htaccess
index.html
folder
games.html
I am using the following code to remove the folder path "folder" from the URL.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]
Would the correct way to "load" the page games.html from the index.html file be:
games
and wouldn't it be a conflictual issue if i have a games.html file in the root folder as well?
Using the method below includes the folder name in the URL. Is it possible to remove the folder name from the URL whilst still being able to link to the page using the entire path?
games
I would appreciate if my provided .htaccess code can be simplified or if there is a better way to write the code.

Assuming you have this rule in site root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ folder/$1 [L]
Let me answer each question as I understand.
Q1. What should be correct html code for having a link?
A1. You can have:
games
Q2. If games.html exists in root directory then which file will be shown?
A2. Apache will present games.html from root because of RewriteCond %{REQUEST_FILENAME} !-f condition which rewrites to folder/ only if file doesn't exist in request.
Q3. What should be the correct rule?
A3. It is better to have your rule as:
RewriteCond %{REQUEST_URI} !^/folder/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ folder/$1 [L]
This way you can avoid infinite looping if rewritten file or directory doesn't exist in folder/ directory.

Related

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

I don't want .html in url of website

My html page is showing .html at the end of url I want to remove that. Can anyone help me out.
My url are like
mysitedoain.com/about.html
..../contact.html
You need to create a file called '.htaccess' in the root directory of your site containing the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I hope this will work! after that you can access your pages without .html.
The answer here seems to be what you're looking for.
tl;dr: create a .htaccess file in your root directory with a rule to rewrite the url, removing the .hmtl
Create a file called '.htaccess' in your root directory and rewrite it with this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
You need to create an .htaccess file. In order to do that, open Notepad on your computer. In Notepad, input the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
In order to save the file, go to File > Save As. For the file name, call it “.htaccess” without the quotes. Change the Save As Type to “All Files.”
Once you have the file saved, upload it to your public_html folder on the server. The last thing you’ll have to do is remove .html from all of your file names.

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]

How does URL without extensions such as HTML work?

For example,
https://stackoverflow.com/questions/ask
ask does not have .html but takes me to the page I want. How does this all work? And where can I learn more about this?
It is made from the .htaccess file where you make rules for your website/server.
By having http://stackoverflow.com/questions/ask as the address the server will interpret it as http://stackoverflow.com/questions/ask.php or http://stackoverflow.com/questions/ask.html...
You can check this site
Add .htaccess file in your root folder of your domain with following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]