-
Hide "index.html". If I'm not mistaken, this solution is fine :
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
You say "fine", however, this does not appear to correlate with your site structure, so it wouldn't appear to do anything? This directive assumes you have index.html
in the document root. But don't you want to remove index.html
and index.php
regardless of where they appear in the URL-path? For example, something like:
RewriteCond %{THE_REQUEST} /index\.(html|php)
RewriteRule (.*)index\.(html|php)$ /$1 [R=301,L]
The condition is to prevent a redirect loop on certain server configs.
This also requires that both index.php
and index.html
are included in your DirectoryIndex.
-
Hide html and php extension. I used this and it worked fine for php but not html...don't know why.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.html [NC,L]
This is a conflict. You have the same RewriteRule
pattern - so which should "win"? The first directive will always "win", the second directive will never happen.
But... is your "site structure" not complete? Your site structure shows a single index.php
in the document root (which is redirected to /
anyway) - there are no .php
files in subdirectories, so why the need to check?
If you do have a mix of .html
and .php
files throughout your site structure, with no discernible pattern then you will need to perform a filesystem check to see if the file exists before rewriting to it (which is relatively expensive). For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
There is no need to backslash-escape a dot (to match a literal dot) when used inside a character class, as it carries no special meaning when used inside the character class (as opposed to elsewhere in the regex).
However, if there is a pattern, then this can be greatly improved.
- Hide pages directory (I don't mind if
/fr/
or /en/
directories are shown) so that URL will be like this (in the end) : http://example.com/fr/a
To clarify, you must first link to example.com/fr/a
.
Then to rewrite all requests to the /pages
subdirectory (and append the .html
extension in the same motion), you can do something like the following:
RewriteRule ^(fr|en)/([^/.]+)$ /$1/pages/$2.html [L]