修改 .htaccess 以将 PHP 重定向为 HTML,除了一个要忽略的页面。

huangapple go评论92阅读模式
英文:

Modify .htaccess for redirect php to html except for one page to be ignored

问题

I use a custom php script (php v.7) and have 5 php pages:
index.php, product1.php, product2.php, product3.php, admin.php.

This htaccess code redirects non-www to www, http to https and php pages redirect to .html format.

Question: how can I redirect from php to html only on 4 of the 5 pages, to add an ignore rule for admin.php? I don't want to have php to html redirection for the admin.php page.

Thank you in advance!

I tried more than 15 variants and none of them worked in 4 hours of searching on stackoverflow.

英文:

I use a custom php script (php v.7) and have 5 php pages:
index.php, product1.php, product2.php, product3.php, admin.php.

<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|jpeg|png|woff|woff2|webp)$">
    Header set Cache-Control "max-age=604800, public, no-cache"
</FilesMatch>
Options -Indexes

This htaccess code redirects non-www to www, http to https and php pages redirect to .html format.

Question: how can I redirect from php to html only on 4 of the 5 pages, to add an ignore rule for admin.php?
I don't want to have php to html redirection for the admin.php page

Thank you in advance!

I tried more than 15 variants and none of them worked in 4 hours of searching on stackoverflow

答案1

得分: 0

You can add RewriteCond %{REQUEST_URI} !^/admin.php$ before RewriteRule to ignore the admin.php page.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/admin.php$
RewriteRule ^(.+?)\.php$ /$1.html [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ /$1.php [L]
英文:

You can add RewriteCond %{REQUEST_URI} !^/admin.php$ before RewriteRule to ignore admin.php page

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/admin.php$
RewriteRule ^(.+?)\.php$ /$1.html [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ /$1.php [L]

huangapple
  • 本文由 发表于 2023年4月10日 21:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定