How to combine Redirect subdirectory to root and remove HTML extension at the same time using .htaccess?

Viewed 71

I tried to combine Redirect subdirectory to root directory and remove .html using .htaccess

But I got a problem with my activity

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RedirectMatch 301 ^/path/$ http://testdomain.com/
# RedirectMatch permanent ^/path/$ http://testdomain.com/
# RedirectMatch 301 ^/path/.*$ http://testdomain.com/
# RewriteRule ^path/(.*)$ /$1 [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

</IfModule>

I didn't have any problem with remove .html from the extension of the link and get the result

but I cannot get the subdirectory link in my root, I tried to clear # from the first 4 lines every time but I didn't get a result

I nee do this like

http://testdomain.com/path/hello.html
to
http://testdomain.com/hello

Now with this code, I can remove the HTML extension and my link status is like

http://testdomain.com/path/hello.html
TO
http://testdomain.com/path/hello

Now I need to clear the "path" folder in my link

Meanwhile, I get the error 404 Not Found, please guide me.

1 Answers

You can replace your rule with this rule:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/path/$1.html -f
RewriteRule ^(.+?)/?$ path/$1.html [L]
Related