How to add trailing "/" to website with .htaccess - html

Hi i'm trying to add a trailing "/" to my website for the past few weeks and every code and website tutorial to edit the .htaccess i've tried has failed to work. All my webpages end with ".html". The first part of my htaccess code forces the use of "https" and "www". What I want is that + when a user clicks on a link on my site to say "a href="help.html" it will load help.html but it will appear in the address bar as "https://www.example.com/help/" I don't care if I need to change all my links to omit the ".html" or if I need to change all my filenames to omit the ".html" extension. I just want that trailing "/" + my www and https force. Here is my code so far:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=302]
Options +MultiViews
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=302,L]
P.S. I am aware of the 302 redirect as i'm testing, once my code is working i'll change to 301. Any help would be appreciated.

Add this and If the URL doesnt have a trailing slash the htaccess will redirect the browser to the URL with a the slash.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Related

Website links won't work with .htaccess code which removes .html extension

I have the following code in my .htacess file for my website to remove '.html' from my urls and to redirect any .html to the same page without it (example.com/home.html to example.com/home)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
However, when I try to click on links that are pointed to other pages on my website, I get a 404 error.
I've tried setting the href to http://www.example.com/page2, /page2 and more but I still get the same error. Any help?
Please save your code and put only the following code in your main directory .htaccess file :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(.*?)\.html[\s?/] [NC]
RewriteRule (.*)$ /%1%2 [R=302,L,NE]
Note: clear your browser cache because you did 301 redirect and test the code

I attempted to remove the .html extension from my pages and now when clicked, navigation goes to another site

*This question is unique because I do not need to remove the .html from the file extensions; I need to have internal links function correctly.
My site is http://purplerosecare.com and I tried creating a .htaccess page with the suggested copy/paste from the SO page: How to remove .html from URL
The code I used is below:
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.proofers.co.uk/new} !(\.[^./]+)$
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.proofers.co.uk/new/$1 [R=301,L]
(The code & .htaccess have both been removed)
The problem I'm facing is whenever I click on a link in the navigation it sends me to http://www.proofers.co.uk/new/. How can I get the site functional again?
Try this in your Root/.htaccess file
#Remove html extensions from files
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]

redirect subfolder to root with <subfolder-name>.html using htaccess

I have website that has links like
domain-name.com/pages/contact-us/
www.domain-name.com/pages/contact-us/
domain-name.com/pages/about-us/
www.domain-name.com/pages/about-us/
and other pages ...
What I'm looking for how i can redirect them permanently to same subfoldername.html using htaccess . for example for all the links above should redirected to
domain-name.com/contact-us.html
www.domain-name.com/contact-us.html
domain-name.com/about-us.html
www.domain-name.com/about-us.html
also for other pages, otherwise if page not found to root index.html .
Thanks in advance.
Try the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/pages/(.*[^/])/?)$
RewriteRule ^(.*)$ /%2.html [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.html [L,R=302]
The first three lines will remove the folder pages. If there is a trailing slash, it will be removed. The last three lines will redirect to /index.html, if the request file does not exist.

Redirection through htaccess from non html to .html extension?

i want to add .html to my html files automatically using htaccess.
As we can remove .html very easily but i want to know can i add .html to a non html file that is located with my website.
for example this is my domain
www.abc.com/name.html
but when i am accessing
www.abc.com/name
It is showing me the same page but not automatically adding the .html extension.
So i want that if i try to open
www.abc.com/name then it automatically shows me www.abc.com/name.html
How to achieve this.
Thanks
You can use this rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://pcsmartcare.com%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ /$1.html [L,R=301]
RewriteCond %{THE_REQUEST} /index\.html?\s [NC]
RewriteRule ^(.*)index\.html?$ /$1 [R=301,L,NC]

how to use .htaccess file in our HTML website? for remove page extention from website

I have a HTML website and I want to remove pages extension (.html) from my website. I've used .htaccess file but I don't know how to use .htaccess file in my website's page.
Anybody please help me.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
No need to add code in any page.Create .htacess file with above content & add file to the root folder of your site(for example, /home/domains/domain.com/htdocs/)
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+html(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.html$ http://www.example.com/$1 [R=301,L]
RewriteRule ^([^/]+/)*index/?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.html-f
RewriteRule ^(.*[^/])$ /$1.html[L]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example/$1 [R=301,L]
Change whole content of .htaccess with above one ,I hope above will helpful.!