.htaccess 什么是正确的htaccess代码为Php 7.4重定向?

pgpifvop  于 2022-11-16  发布在  PHP
关注(0)|答案(2)|浏览(125)

有人能帮我把php页面重定向到html(只在根目录,没有子文件夹)吗?我尝试了几十个解决方案,但都没有成功。下面的代码重定向到所有地方(包括子文件夹)我不希望子目录受到影响。我的域:https://www.example.com/提前感谢!

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+?)\.php$ /$1.html [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ /$1.php [L]
</IfModule>

<FilesMatch ".(ico|css|flv|js|gif|jpeg|png|woff|woff2|eot|svg|ttf|webp)$">
    Header set Cache-Control "max-age=604800, public, no-cache"
</FilesMatch>
u0sqgete

u0sqgete1#

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
pu3pd22g

pu3pd22g2#

有这样的话:

RewriteEngine On

# add www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# external redirect to .html from .php request
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^./]+?)\.php$ /$1.html [R=301,L,NC,NE]

# internal rewrite to .php from .html if corresponding php exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)\.html$ $1.php [L,NC]

<FilesMatch ".(ico|css|flv|js|gif|jpeg|png|woff|woff2|eot|svg|ttf|webp)$">
    Header set Cache-Control "max-age=604800, public, no-cache"
</FilesMatch>

请注意,[^./]将匹配任何不是点和/的字符,因此仅在根目录中匹配,跳过所有子目录。

相关问题