Search code examples
.htaccessmod-rewritebrowser-historymodx

How can I get my website to redirect .html pages to the index.php using advanced mod_rewriting


if you look at

http://ecocool.zadesigns.com

click on help link and it goes to

http://ecocool.zadesigns.com/help

this works, but if you physically type "http://ecocool.zadesigns.com/help.html"

it does not work.

how can i modify the htaccess file so that if i type

http://ecocool.zadesigns.com/help.html

into the address bar it removes the .html from the URL, loads the index.php file and then sends me to the correct page.

Here is the htaccess file.

 <ifModule mod_rewrite.c>
     Options +FollowSymLinks
     RewriteEngine on
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} !index
     RewriteRule ^(.*)$ index.php?q=$1 [NC,L,QSA]
 </ifModule>

Solution

  • First rule redirects from the html to directory style, we don't use any condition because we don't want to check if the file exists or not.

    Second rule will redirect the directory like format to the index.php unless it is index.php which it then does nothing.

    So basically if you click on the link:

    http://ecocool.zadesigns.com/anything.html
    

    It will make the link look like this to the user:

    http://ecocool.zadesigns.com/anything/
    

    And will then internally show the index.php?q=anything result.

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(.*)\.html$ /$1/ [R=301,NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !index\.php
    RewriteRule ^([^/]+)/?$ /index.php?q=$1 [L,QSA]
    

    If you don't mind having the URL display the .html and just want to serve the index.php on its place then use:

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} .*\.html$
    RewriteRule ^(.*)\.html$ /index.php?q=$1 [L,QSA]
    

    So basically if you click on the link:

    http://ecocool.zadesigns.com/anything.html
    

    It will internally show the index.php?q=anything result and the URL will remain:

    http://ecocool.zadesigns.com/anything.html