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将文件uri重写为file-name.html或file-name.php(以存在的为准)_.htaccess_Rewrite_Url Rewriting - Fatal编程技术网

.htaccess将文件uri重写为file-name.html或file-name.php(以存在的为准)

.htaccess将文件uri重写为file-name.html或file-name.php(以存在的为准),.htaccess,rewrite,url-rewriting,.htaccess,Rewrite,Url Rewriting,目标:使浏览器重写为file-name.php(如果存在);else return file-name.html-访问者是否键入了以下url之一: http://mydomain.com/file-name http://mydomain.com/file-name.html http://mydomain.com/file-name.php 在我的.htaccess根目录文件中成功地使用了以下规则: # REWRITE FILE URI TO file.php IF EXISTS Option

目标:使浏览器重写为file-name.php(如果存在);else return file-name.html-访问者是否键入了以下url之一:

  • http://mydomain.com/file-name
  • http://mydomain.com/file-name.html
  • http://mydomain.com/file-name.php
  • 在我的.htaccess根目录文件中成功地使用了以下规则:

    # REWRITE FILE URI TO file.php IF EXISTS
    Options Indexes +FollowSymLinks +MultiViews
    Options +ExecCGI
    RewriteEngine on
    RewriteBase /
    # parse out basename, but remember the fact
    RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
    # rewrite to document.php if exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [S=1]
    # else reverse the previous basename cutout
    RewriteCond %{ENV:WasHTML} ^yes$
    RewriteRule ^(.*)$ $1.html
    
    然而,我已经在根目录下安装了WP,和预先存在的网站一起,这些规则不再有效

    工作原理:
    文件名
    被重写为
    文件名.html
    文件名.php
    -无论哪个文件存在

    什么不起作用:
    文件名.html
    不会被重写为
    文件名.php
    ,即使没有
    文件名.html
    文件名.php
    。另外,当没有
    文件名.php
    但有
    文件名.html
    时,
    文件名.php
    不会重写为
    文件名.html

    整个.htaccess的当前状态:

    # BEGIN WP MULTISITE RULES
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]
    # END WP MULTISITE RULES
    
    # REWRITE FILE URI TO file.php IF EXISTS
    Options Indexes +FollowSymLinks +MultiViews
    Options +ExecCGI
    RewriteEngine on
    RewriteBase /
    # parse out basename, but remember the fact
    RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
    # rewrite to document.phtml if exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [S=1]
    # else reverse the previous basename cutout
    RewriteCond %{ENV:WasHTML} ^yes$
    RewriteRule ^(.*)$ $1.html
    

    有什么建议吗

    Quick overview告诉我们,您的原始规则很可能永远无法到达,因为WP规则应该拦截所有请求

    具有这些条件的此行
    RewriteRule^-[L]
    将中止对任何现有文件或文件夹的重写,而此行
    RewriteRule。index.php[L]
    将拦截/重定向所有请求到
    index.php

    如果您将规则移到WordPress one之上,那么它将再次工作


    要将不存在的.php文件的请求重写为.html文件,请使用以下规则:

    # rewrite non-existing .php file to existing .html
    RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.html -f
    RewriteRule ^(.+)\.php$ $1.html [L,PT]
    
    将其置于规则之下(但高于WP)。该规则将检查.php文件是否不存在,并且只有在存在.html文件时才会进行重写。如果两个文件都不可用,则不会发生任何事情


    请记住,由于这些检查以及规则位于重写链的顶端,因此每个对.php文件(甚至是WP页面)的请求都会对该规则进行评估,这可能会给非常繁忙的服务器带来额外的压力。理想情况下,您首先希望有适当的URL,这样就不需要进行此类操作

    是的,我把规则移到WP上面了,它又起作用了。大部分。也就是说,“file name”返回“file name.html”或“file name.php”-以存在的为准,“file name.html”将返回“file name.php”(如果存在)。但如果只有“file name.html”而没有“file name.php”,则“file name.php”将返回404。在这种情况下,是什么原因导致将“file name.php”重写为“file name.html”?谢谢,@LazyOne。我尝试将您的规则置于我的规则之下,WordPress规则之上,但是当没有“file name.php”但有“file name.html”时,“file name.php”仍然返回404。@user864464我已经在本地Apache上测试了该规则——效果绝对不错。可能有其他规则会影响它。。也许你的WP安装——我不能肯定。最好的方法是启用重写调试(
    rewriteloglevel9
    )并检查重写日志以查看服务器上发生的情况(我无法在此处复制您的环境)。正如我所说,它在这里工作得很好。。但在禁令和其他规则中,考虑到您的Apache设置可能与我的完全不同,它的工作方式可能会有所不同。@user864464我也尝试过您的原始规则+我的规则-再次正常工作。一定是有WP规则的东西。。或者完全不同的东西(我不知道它可能是什么)。为什么您使用
    {DOCUMENT\u ROOT}
    而不是
    {REQUEST\u FILENAME}
    ?我进行了替换和测试,但在跟踪链接时仍然得到404,但实际文件名为“file name.html”(uri为)。当预先存在的网站被淘汰时,这些规则将被删除,以减轻服务器负载。:)@user864464因为我复制了第二个
    RewriteCond
    作为第一个,并且没有修改任何东西——路径无论如何都是相同的(在一些罕见的情况下(必须是由于Apache特定的配置),使用
    %{DOCUMENT\u ROOT}
    组装目标文件名甚至比使用
    {REQUEST\u FILENAME}效果更好
    直接输入)。完全删除WP规则(暂时删除并再次测试)。我建议(当然,如果您可以的话)尽可能多地删除并测试它是否有效,然后添加其他内容并再次测试,直到您看到它失败的地方。