htaccess URL 中存在空字符串问题

问题描述 投票:0回答:1

我希望用户只能访问domain.com或domain.com/。之后的任何内容,甚至是domain.com/index.php,都应该重定向到domain.com。

我有这个.htaccess

RewriteBase /
RewriteEngine On

Options -Indexes
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} =on
RewriteRule ^$ index.php [L]

RewriteCond %{HTTPS} =on
RewriteRule ^([^/]+)$ 404.php?cp=$1 [L,QSA]

问题是参数永远不会为空。因此 404.php 总是被调用。如果我访问domain.com,它会重定向到

404.php?cp=index.php
。如果我删除index.php规则,它会重定向到
404.php?cp=index.php
。如果我删除 index.php 文件和规则,则会收到服务器错误。如果我允许
index.php
,那么它可以工作并重定向到index.php,但这不是我想要的。即使最终调用了index.php,用户也不应该能够调用index.php或任何文件,或者在域之后添加任何内容。所以基本上我只想允许内部重定向到index.php,前提是它是由htaccess 文件指示的,而不是由用户指示的。 我该如何解决这个问题?

php .htaccess redirect url-rewriting
1个回答
0
投票

以下重定向会将任何不是

/
的请求重定向到
/
。 domain.com 和domain.com/ 是同一个东西。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$ 
RewriteRule . / [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.