How to remove .html from URL?

前端 未结 12 1578
轻奢々
轻奢々 2020-11-22 03:02

How to remove .html from the URL of a static page?

Also, I need to redirect any url with .html to the one without it. (i.e. www.exam

12条回答
  •  时光说笑
    2020-11-22 03:25

    I think some explanation of Jon's answer would be constructive. The following:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    checks that if the specified file or directory respectively doesn't exist, then the rewrite rule proceeds:

    RewriteRule ^(.*)\.html$ /$1 [L,R=301]
    

    But what does that mean? It uses regex (regular expressions). Here is a little something I made earlier...

    I think that's correct.

    NOTE: When testing your .htaccess do not use 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See https://stackoverflow.com/a/9204355/3217306

    Update: I was slightly mistaken, . matches all characters except newlines, so includes whitespace. Also, here is a helpful regex cheat sheet

    Sources:

    http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-f-d/2034/2

    https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

提交回复
热议问题