Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
Php 如何将mod_重写规则应用于目录中的文件?_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php 如何将mod_重写规则应用于目录中的文件?

Php 如何将mod_重写规则应用于目录中的文件?,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我试图将htaccess文件中的重写条件应用于我网站上特定目录中包含的所有页面,但不应用于目录索引本身 最后,我尝试从特定目录中的页面中删除.php文件扩展名 我现在拥有的: https://www.example.com/directory/page-1.php https://www.example.com/directory/page-1 我想要达到的最终结果: https://www.example.com/directory/page-1.php https://www.exam

我试图将htaccess文件中的重写条件应用于我网站上特定目录中包含的所有页面,但不应用于目录索引本身


最后,我尝试从特定目录中的页面中删除.php文件扩展名

我现在拥有的:

https://www.example.com/directory/page-1.php
https://www.example.com/directory/page-1
我想要达到的最终结果:

https://www.example.com/directory/page-1.php
https://www.example.com/directory/page-1
此外,如果用户试图访问该页面的.php版本,它将301重定向到新版本,而不使用.php文件扩展名

示例匹配:

https://www.example.com/directory/page-1.php
https://www.example.com/directory/page-2.php
不应匹配:

https://www.example.com/directory/
以下是我目前掌握的情况:

RewriteCond %{REQUEST_URI} \/directory\/[^\s]+$

重写规则应该如何编写

可能有助于我们找到重写规则的表达式如下所示:

^https?:\/\/(www\.)?example\.com\/directory\/[^\s]+\.php$
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} (.*\/directory\/)([^\s]+)\.php [NC]
    RewriteRule (.*directory\/)([^\s]+)\.php   $1$2 [L,R=301]
</IfModule>
这将使我们不想要的URL失败,并通过想要的URL

我们的规则可能如下所示:

^https?:\/\/(www\.)?example\.com\/directory\/[^\s]+\.php$
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} (.*\/directory\/)([^\s]+)\.php [NC]
    RewriteRule (.*directory\/)([^\s]+)\.php   $1$2 [L,R=301]
</IfModule>

重新启动发动机
重写cond%{REQUEST\u URI}(.*\/directory\/)([^\s]+)\.php[NC]
重写规则(.*directory\/)([^\s]+)\.php$1$2[L,R=301]
重写规则测试 您可以在中测试重写规则

正则表达式电路 可视化正则表达式:


每次更改
htaccess
文件时,我们可能还希望清除浏览器缓存。

您可以在
子文件夹中的htaccess中使用以下规则,如果它不存在,请创建一个:

 RewriteEngine on
 #rule for /subfolder/htaccess 
#redirect /subfolder/file.php to /file
#The condition bellow prevents infinite loop/Too many redirects  error
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /subfolder/$1 [L,R]
#internally map /subfolder/file to /subfolder/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /subfolder/$1.php [L]
如果要改用根htaccess文件,请添加以下内容:

 RewriteEngine on
 #rule for root/.htaccess 
#redirect /subfolder/file.php to /file
#The condition bellow prevents infinite loop/Too many redirects  error
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteRule ^subfolder/(.+)\.php$ /subfolder/$1 [L,R]
#internally map /subfolder/file to /subfolder/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^subfolder/(.+)/?$ /subfolder/$1.php [L]

最后,我尝试从特定目录中的文件中删除.php文件扩展名。我编辑了我的帖子来反映这一点。我将您的建议包括在内,只是做了一点小改动。我已经按照建议使用madewithlove工具进行了测试。我将使用302重定向在我的live server上进行测试,直到解决所有错误。在live server上,我似乎遇到了URL路径问题。它试图请求:您的服务器上运行的是什么版本的Apache?服务器上的跨站点(后续)帖子错误,并给出可接受的答案: