Wednesday 23 October 2019

2021 Apache2 remove .html ending on website

webpage redirect to index page should just be / instead of index.html
the following rewrite does not require to change all redirect links from xxx.html to xxx at all
just change the redirect to index.html to /


Errors :
"htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration"
a)check apache error log
b)vim /var/log/apache2/error.log

Enable  rewrite module
a2enmod rewrite
systemctl restart apache2


!!!!! When below procedure is done, makesure to clean browser cache @2021 to see effect, use inspector then right click refresh, then click empty cache and hard reload
1) Check apache2 config

  • Go to /etc/apache2/apache2.conf 
  • edit <Directory /var/www>....
    • to be 

              <Directory /var/www/>
                  Options Indexes FollowSymLinks
                  AllowOverride All // allow .htacess override
                 Require all granted
             </Directory>
Restart apache2
sudo service apache2 restart


2) create .htaccess file in your /var/www/domain(that services website)

and add
RewriteEngine on


RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

to the file

// This does not require to remove any .html links in your web page
https://stackoverflow.com/questions/5730092/how-to-remove-html-from-url

.htaccese file need read permission by webserver



 https://stackoverflow.com/questions/5730092/how-to-remove-html-from-url


Go daddy lamp server :
In .htaccess file change the following :


#example.com/page will display the contents of example.com/page.html

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.+)$ $1.html [L,QSA]



#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]




For Godaddy add:



 Options -MultiViews



For http to https redirect



RewriteCond %{SERVER_PORT} 80

RewriteCond %{HTTP_HOST} ^(www\.)?citycentreupcc\.ca

RewriteRule ^(.*)$ https://www.citycentreupcc.ca/$1 [R,L]




Full code 

Options -MultiViews

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?citycentreupcc\.ca
RewriteRule ^(.*)$ https://www.citycentreupcc.ca/$1 [R,L]


#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#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]

No comments:

Post a Comment