Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

<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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance!

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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading