How to remove the word 'index' in the url [.htaccess]?

纵饮孤独 提交于 2019-12-11 12:53:16

问题


Currently, I am using the below code to remove the word 'index' in my urls - however, the issue is that it allows for two versions: one without and one with [the word] - hence, I have referenced the one without (in the HTML) - but the user still might type the one with the word.

How can I completely wipe the word 'index' from the index page:http://example.com/index[.html] goes to http://example.com/index but should really go to http//:example.com

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/index$ $1/ [R=301]
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

回答1:


You can use:

RewriteEngine On

RewriteCond %{THE_REQUEST} /index [NC]
RewriteRule ^(.*?)index(?:\.html)?$ /$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/index$ $1/ [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]


来源:https://stackoverflow.com/questions/30684749/how-to-remove-the-word-index-in-the-url-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!