Rewrite rules for .htaccess - html

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

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

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]

Permanent redirect after removing html extension in htaccess

I have these htaccess rules to remove html extension from urls:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
It works fine but I can still access from the URL with extension. How to make it so that when a user click the url with extension, will be redirected with a permanent (301) redirect to the extensionless url?
Thanks
Just add the little snippet at the end which indicates it is a 301 redirect.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [R=301,L]

Removing duplicate copies (html extension vs non-html)

Is there any way to have the first URL redirect to the second URL?
http://www.example.com/podcast/episode.html
http://www.example.com/podcast/episode
Is there any way to force the .html extension to redirect to the non-html version of the URL, so as not to show up as duplicate copies of the same page.
My htaccess code at the moment is:
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#shtml
AddType text/html .html
AddHandler server-parsed .html
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
Is this possible?
Not entirely sure if I understand the question, but if you want anything ending in .html to redirect to the same URL without the .html, this should do the trick:
RewriteRule ^/(.*)\.html$ /$1 [R=301,L]
This is how you can do the redirection from html to non-html only.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]

Trouble removing .html URL extension using .htaccess

Trying to remove .html extensions from the site using .htaccess. So for example: www.mysite.com/charts.html would become www.mysite.com/charts
The following script is in the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
But when the url without the .html extension is entered in the browser, it shows a 403 Forbidden error. Any help would be appreciated.
I found this solution elsewhere:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
sources:
1) http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox
2) http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/