Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解我的.htaccess文件以便将X重写为X.php_.htaccess_Mod Rewrite - Fatal编程技术网

理解我的.htaccess文件以便将X重写为X.php

理解我的.htaccess文件以便将X重写为X.php,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我在SiteGround.com上有一个PHP文件的网站,希望链接看起来像“example.com/about”,而不是“example.com/about.PHP”。我还希望在访问“example.com”而不是“example.com/index”时加载我的index.php 所以我做了一些搜索,这就是我想要的: RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(

我在SiteGround.com上有一个PHP文件的网站,希望链接看起来像“example.com/about”,而不是“example.com/about.PHP”。我还希望在访问“example.com”而不是“example.com/index”时加载我的
index.php

所以我做了一些搜索,这就是我想要的:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php 
但是Web服务器已经有一个默认的
.htaccess
文件,该文件包含以下内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule . /index.php [L]
</IfModule>
在我的HTML中,我有:

<a href="about" .... ></a> <!-- link to about.php -->
<a href="/" ....></a> <!-- link to index.php -->


是这样吗?这似乎对我不起作用。

首先,行:

RewriteCond %{REQUEST_FILENAME}\.php -f
不是原始
.htaccess
文件的一部分。不可能,它在哪里没有意义,也许是你自己在实验时添加的

除了那一行之外,您的
.htaccess
文件是通过
index.php
处理所有内容的标准方法。可能是为了WordPress。所以你需要决定你想做什么。您是希望通过
index.php
完成所有操作,还是希望使用单独的
.php
文件并在服务器端添加扩展名?或者你想两者兼而有之?(在这种情况下,需要编写组合方法)您是否仍在使用WordPress?也许你可以删除这些规则

让我知道,我可以更新答案,如果你需要更多的信息

更新

您的规则可以修改为:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,NE]
这应该可以解决它,我认为
请求
行上的
\.
打破了它,其余的都是改进

如果这不起作用,那么可能是mod_rewrite未启用,或者
.htaccess
文件未启用。您可以通过在一行上放置一些垃圾来测试它,并查看是否出现500错误


如果根目录不提供index.php,那么您只需要添加一个
DirectoryIndex

谢谢您的回复。我想使用单独的
.php
文件,并在服务器端添加扩展名。我希望用户能够访问像“www.example.com/about”这样的url,并让服务器返回
about.php
。除了root之外,我想让服务器返回
index.php
,但用户认为“www.example.com”有效!非常感谢。你能向我解释一下你的规则在做什么,为什么我的规则是错误的吗?我希望将来能更好地了解这一点。我能推荐的最好的东西就是看一看。它有每个指令的详细信息,并解释了每个指令所发生的一切(所以请看,和)。如果你还想要更多。
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,NE]