Ad

Redirecting /page.html To /page Doesn't Work Inside Subdirectory

- 1 answer

I'm trying to redirect from /page.html to /page inside of a subdirectory, but it doesn't work.

  • What I do:http://example.com/subdirectory/page.html
  • What I get:http://example.com/page
  • What I want:http://example.com/subdirectory/page

.htacces file:

RewriteEngine On

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

How can I get this to work?

Ad

Answer

Use this code for .html hiding in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /$1.html [L]
Ad
source: stackoverflow.com
Ad