如何防止打开 /index.php 或 /index.html URL?

如何防止打开 /index.php 或 /index.html URL?
DirectoryIndex index.html index.php

# -- INIT
RewriteEngine on
RewriteBase /

# -- PRODUCTION
# RewriteCond %{HTTP_HOST} ^www.assist\.loc$ [NC]
# RewriteRule ^(.*)$ http://assist.loc/$1 [R=301,L]

# -- SEO index.html // index.php ??????????? DONT KNOW ????????
RewriteCond %{THE_REQUEST} ^.*/index\.(php|html)
RewriteRule .* / [R=301,L]

# -- HACKING
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* /index.php [F]

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

# -- FILES
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

您好,有人能帮我编写 .htaccess 文件吗?我想添加 # -- SEO 指令,以便从 index.php 或 index.html 文件 301 重定向到“/”。

但是现在,我已从所有路由重定向到“/”,或者从 index.php 或 index.html 重定向时循环。

需要更正#--SEO部分。

答案1

如果您想将任何 index.{anything} 重定向到根路径:

# -- DEFAULT DOCUMENT
DirectoryIndex index.html index.php

# -- INIT
RewriteEngine on

# -- REDIRECT ANY INDEX PAGE TO ROOT
RewriteCond %{Request_URI} /index\.
RewriteRule .* / [R=301]

# -- HACKING
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* /index.php [F]

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

# -- REDIRECT REQUEST FOLDER TO /INDEX
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule .* /index.php [L]

例如 :

  • www.test.com/index.php=> 将显示 www.test.com/
  • www.test.com/test/index.php=> 将显示 www.test.com/
  • www.test.com/any/folder/index.htm=> 将显示 www.test.com/

这会将任何索引重定向到他的文件夹(如果文件夹存在)

# -- REDIRECT ANY INDEX PAGE TO REQUEST FOLDER
RewriteCond %{Request_URI} /index\.
RewriteRule (.*)/index\. $1 [R=301]

# -- REDIRECT REQUEST FOLDER TO IT INDEX
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule (.*)/index\. %{DOCUMENT_ROOT}$1/index.php [L]

例如 :

  • www.test.com/index.php=> 将显示 www.test.com/
  • www.test.com/test/index.php=> 将显示 www.test.com/test/
  • www.test.com/any/folder/index.htm=> 将显示 www.test.com/any/folder/

相关内容