2017-06-13 19 views
0

实际的URL www.example.com/index.php 在重写小册子以删除.php后,它变成www.example.com/index。 唯一的问题是www.example.com/index.php仍然可以访问。 如何摆脱这个? 重写规则删除我使用的.php。Index.php在将其更改为索引后仍然可以访问.htaccess

**RewriteRule ^index$ index.php [NC,L]** 
+2

你是什么意思与‘仍可访问’?重写不会阻止用户成功键入'index.php',它只会(内部)重写'index' _to_'index.php' – Jeff

+0

然后,请告诉我我如何阻止用户访问index.php。如果有人输入www.example.com/index.php而不是www.example.com/index,则应显示未找到错误404网址。 – adii

回答

0

反过来:RewriteEngine将用户提供的URL转换为可以访问的本地文件(简化版)。

如果用户打开“http://example.com/test”,重写引擎将通过添加扩展名将其转换为“test.php”。

我假设你希望用户再也看不到“.PHP”在浏览器中,所以这里是一个解决方案:

RewriteEngine On 

# If is has php extension, 
# let browser redirect to non-php version 
RewriteRule ^(.*)\.php$ /$1 [L,END,R] 

# If the file does not exist, add php extension 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ $1.php [L,END] 
+0

在此服务器上找不到请求的URL。如果您手动输入网址,请检查拼写并重试。 之后面对这个错误。执行以上规则后。 – adii

+0

您尝试的URL路径到底是什么,您希望加载的PHP文件是什么? – user3429660

+0

感谢问题解决了。 – adii

相关问题