Steve Robbins
Steve Robbins

Reputation: 13822

Redirect PHP to HTML, but have HTML point to PHP

Consider the following rules

RewriteRule ^index\.html$ index.php [L]
RewriteRule ^index\.php$ index.html [R=301]

I want .html to use the .php file, but I want the url to have the .html extension always.

The above rule creates a redirection loop. What's the right way to do this?

Upvotes: 1

Views: 138

Answers (1)

Jon Lin
Jon Lin

Reputation: 143966

You can either check against the actual request, or prevent looping entirely.

  1. Option 1, check against request:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
    RewriteRule ^index\.php$ index.html [R=301]
    

    This makes it so if the actual request isn't index.php, the redirect won't happen.

  2. Option 2, prevent rewrite engine from looping entirely. Add this to the top of your htaccess file:

    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    

    The downside with this is that you may have rules that actually want to loop.

Upvotes: 1

Related Questions