I was looking for a solution on many topics (E.g. 1, 2, 3, 4, 5, 6, 7,8). Unfortunately, I did not find a good solution.
Overall, I want to achieve the following effect: no www.
at the beginning of the URL, no .html
or /
at end of the URL, URL containing the full path to the file (but without showing its extension).
Examples: URL entered by the user >> redirection (the contents of this file should be loaded) >> address that can be seen in the address bar after redirection
www.example.com
orexample.com
orexample.com/
orexample.com/index.html
orexample.com/index
>> index.html >> http://example.comwww.example.com/abc
orwww.example.com/abc.html
>> abc.html >> http://example.com/abcwww.example.com/abc/abc1
orwww.example.com/abc/abc1.html
>> abc1.html >> http://example.com/abc/abc1
File structure on my server:
Main directory:
index.html
abc.html
xyz.html
.htaccess
layout (there are .css, .jpg, .png ... files)
abc:
abc1.html
abc2.html
My .htaccess file (version 1):
# rewrite engine initiation
RewriteEngine on
RewriteBase /
#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !^.+\.html$
RewriteRule ^(.+[^/])$ $1.html
#301 from example.com/page.html to example.com/page
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]
Almost redirections work good, with the exception of these redirections:
www.example.com/abc.html
>> abc.html >> http://example.com/abc (in the address bar is still http://www.example.com/abc.html)www.example.com/abc/abc1.html
>> abc1.html >> http://example.com/abc/abc1 (in the address bar is still http://www.example.com/abc/abc1.html)
My .htaccess file (version 2):
# rewrite engine initiation
RewriteEngine on
RewriteBase /
#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]
RewriteRule ^index$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index$ /$1/ [R=301,L]
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
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]
Now almost redirections work good, with the exception of these redirections:
www.example.com/abc
orwww.example.com/abc.html
>> abc.html >> http://example.com/abc (I get 403 error - abc is directory, but I also have file abc.html)
...and there are several situations when I get Internal Server Error (500). This happens when I add /
and any wrong characters at the end of the URL.
www.example.com/index/anycharacter
www.example.com/abc/abc1/anycharacter
www.example.com/xyz/anycharacter
When at the end of the URL is .html
before /
and any wrong characters, I get a 404 error. This is correct. Examples:
www.example.com/index.html/anycharacter
www.example.com/abc.html/anycharacter
www.example.com/abc/abc1.html/anycharacter
www.example.com/xyz.html/anycharacter
I get 404 error also with this URL (this is also correct because it is about the abc directory):
www.example.com/abc/anycharacter
How should I change my code to make everything work correctly? I've been trying to do this for a very long time. I'm counting on your help!