Trailing slashes problem

后端 未结 3 1720
滥情空心
滥情空心 2020-12-19 08:09

When I type this \"http://example.com/Hello%20There/\" , it displays the index page wich is : \"http://example.com/Hello%20There/index.html\" .

Well, what I want to

3条回答
  •  生来不讨喜
    2020-12-19 08:54

    You can make a character optional by appending the ? quantifier to it like this:

    RewriteRule ^([^/]+)/?$ $1/index.html
    

    Now both /foobar and /foobar/ would be rewritten to /foobar/index.html.

    But it would be better if you use just one spelling, with or without the trailing slash, and redirect the other one:

    # remove trailing slash
    RewriteRule (.+)/$ /$1 [L,R=301]
    
    # add trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .*[^/]$ /$1/ [L,R=301]
    

    These rules either remove or add a missing trailing slash and do a permanent redirect.

提交回复
热议问题