简体   繁体   中英

Return a 404 if trailing slash is entered after file extension

I am using AddType application/x-httpd-php .htm .html on static .html pages to serve as PHP in .htaccess file.

The problem is when someone enters a trailing slash after the .html file extension, the URL can extend to anything causing huge number of junk/duplicate pages.

eg, www.example.com/file.html would serve the same page as www.example.com/file.html/file.html or www.example.com/file.html/anything-here-bla-bla

If someone enters www.example.com/file.html/anything , for example, I want to redirect to a 404 page.

Just found the solution myself. Here is what will do the desired.

RewriteEngine On RewriteCond %{THE_REQUEST} /([^.]+).html/ [NC] RewriteRule ^(.*)$ /404.shtml [NC,R,L]

This will redirect visitors/googlebot to 404.shtml when a trailing slash is added after the file extension (.html) in this case.

Hope this will help others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM