Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Apache 将文件扩展名从.htm更改为.php的最佳方法是什么_Apache_.htaccess_File Extension - Fatal编程技术网

Apache 将文件扩展名从.htm更改为.php的最佳方法是什么

Apache 将文件扩展名从.htm更改为.php的最佳方法是什么,apache,.htaccess,file-extension,Apache,.htaccess,File Extension,我需要将Web服务器中的一些htm文件更改为.php文件。这些页面从不同的地方链接。我不希望他们显示一个文件未找到错误时,有人试图访问他们 例如,如果index.htm需要更改为index.php,我可以写一个重定向吗 redirect 301 /index.html index.php 在我的htaccess文件中?这是最好的方法吗 谢谢 Prady您可以使用mod_重写。类似的方法可能会奏效: RewriteEngine on RewriteRule index.php inde

我需要将Web服务器中的一些htm文件更改为.php文件。这些页面从不同的地方链接。我不希望他们显示一个文件未找到错误时,有人试图访问他们

例如,如果index.htm需要更改为index.php,我可以写一个重定向吗

     redirect 301 /index.html index.php
在我的htaccess文件中?这是最好的方法吗

谢谢


Prady

您可以使用mod_重写。类似的方法可能会奏效:

RewriteEngine on
RewriteRule index.php index.html
您可以使用正则表达式。见:
您可以使用mod_重写。类似的方法可能会奏效:

RewriteEngine on
RewriteRule index.php index.html
您可以使用正则表达式。见:

重定向301/index.html index.php

如果您是通过POST发送数据,则此操作不起作用。除了重命名文件和重写链接之外,任何事情都是一种丑陋的、暂时的黑客行为。FWIW,您可以通过将每个“页面”放在自己的目录中,并在URL中引用目录,并通过directoryIndex配置指定默认文件来避免这种情况

您没有提到这是在什么操作系统上运行的-如果是Linux/Unix/BSD,那么您可以使用sed来更改所有绝对链接(使用grep查找可能需要手动编辑的相对链接)

find /path/to/root/of/docs -type f -exec sed -ibkup 's/\/index.html/\/index.php/g' {} \;
find /path/to/root/of/docs -type f -exec grep -l 'index.html' {} \;
重定向301/index.html index.php

如果您是通过POST发送数据,则此操作不起作用。除了重命名文件和重写链接之外,任何事情都是一种丑陋的、暂时的黑客行为。FWIW,您可以通过将每个“页面”放在自己的目录中,并在URL中引用目录,并通过directoryIndex配置指定默认文件来避免这种情况

您没有提到这是在什么操作系统上运行的-如果是Linux/Unix/BSD,那么您可以使用sed来更改所有绝对链接(使用grep查找可能需要手动编辑的相对链接)

find /path/to/root/of/docs -type f -exec sed -ibkup 's/\/index.html/\/index.php/g' {} \;
find /path/to/root/of/docs -type f -exec grep -l 'index.html' {} \;