简体   繁体   中英

.htaccess for /index.html = /

I would like Google, Twitter and Google+ to treat

http://example.com http://example.com/ and http://example.com/index.html as one and the same URL.

Redirect 301 http://example.com http://example.com/ works
Redirect 301 http://example.com/index.html http://example.com/ does nothing (two different urls are still seen)
Redirect 301 /index.html / results in a redirect loop

I'd be most grateful to anyone who might be able to give me the definitive way to write that has redirect.

You can use this code in your DOCUMENT_ROOT/.htaccess file:

DirectoryIndex index.html
RewriteEngine On

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

Also note that http://example.com/ and http://example.com are same URL for a web server and browser. Browsers usually strip trailing slash even before sending request to web server thus making http://example.com/ to http://example.com

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