【问题标题】:Htaccess redirect php files to html in rootHtaccess 将 php 文件重定向到根目录下的 html
【发布时间】:2017-10-16 06:19:32
【问题描述】:

我尝试将index.php加上系统页面如404.php、403.php等重定向到ROOT目录下各自的.html页面(index.html、404.html、403.html等) .

问题是 Stackoverflow 上提供的建议解决方案似乎不适用于我的情况。由于技术要求,PHP文件必须保留在根目录下。

.htaccess 中的规则

RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . index.php [L]

</IfModule>
# -- concrete5 urls end --

<IfModule mod_headers.c>    
    Header set Access-Control-Allow-Origin *
</IfModule>
#Header set Access-Control-Allow-Origin *

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    在您的主目录.htaccess 文件中尝试此代码:

    RewriteEngine On
    RewriteRule ^(index|404|403)\.php$   /$1.html [L,R=302]
    

    上面的代码只会在根目录中重定向提到的页面,如果想添加子目录到其中的任何一个,只需像sub/indexsub/404这样改变它。

    我适合没问题,将302更改为301以获得永久重定向

    【讨论】:

    • 它部分起作用了。现在一切都重定向到 index.html。但是,如果我访问 php 相关子目录(根/文件夹)中的文件夹,它也会重定向回根目录中的 index.html 而不是该子目录中的 index.php。
    • 我确定它只适用于root,但您可能在子目录中有其他规则,等我修改它
    • 它应该只能在 root 中工作,如果你在 .htaccess 中对子文件夹或该子文件夹有任何规则,请告诉我
    • 子文件夹中没有 .htaccess 文件。我确实使用现有 .htaccess 文件的内容编辑了我的原始帖子。那里的某些东西可能会干扰您的解决方案。
    • 我认为你使用的是 aptche 2.4 所以,RewriteOptions 继承会在任何规则之前将根规则应用到子目录,取出并清除浏览器缓存并测试它
    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 2021-09-03
    • 2011-01-19
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2011-01-14
    相关资源
    最近更新 更多