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]
Related
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.
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.
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.
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]
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