Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:39:20+00:00 2026-06-11T10:39:20+00:00

Using .htaccess, I’d like to redirect files that do not exist to a controller

  • 0

Using .htaccess, I’d like to redirect files that do not exist to a controller page, and rewrite the extension of .php files that do exist to an .html extension. If a file exists and is an .html page, I’d like it to remain the same. Every time I try to inject the rewrite rule from .php to .html, I seem to mess up the redirect to the controller page. So I’m not sure where to go from here:

Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 404 /404.php

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ mycontroller.php [L,QSA]
</IfModule>

Any help I’d be most grateful for.

Edit

I seem to have found most of the answer here (but I have to leave out the ReweriteBse or it doesn’t work). The biggest issue is that now, my existing .html files don’t work, it only serves my .php files with .html extensions and directs all else to the controller. Existing .html files go to my 404 page. I’d like to know how I can keep my existing .html files intact. My new code as follows:

  RewriteEngine on

  RewriteCond %{THE_REQUEST} (.*)\.php  
  RewriteRule ^(.*)\.php $1.html [R=301,L]  

  RewriteCond %{THE_REQUEST} (.*)\.html  
  RewriteRule ^(.*)\.html $1.php [L]  

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ mycontroller.php [L,QSA]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:39:21+00:00Added an answer on June 11, 2026 at 10:39 am

    I’m sure there are better ways to do this, but the following works for my needs. The other answers provided didn’t seem to work despite my attempts.

    Options -Indexes
    Options +FollowSymLinks
    DirectoryIndex index.php
    ErrorDocument 404 /404.php
    
    <IfModule mod_rewrite.c>
      RewriteEngine on
    
      # Check if .html file already exists -- if so, do nothing
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteCond %{THE_REQUEST} (.*)\.html  
      RewriteRule ^.*$ - [NC,L] 
    
      # Check if .php file already exists -- if so, rewrite extension to .html
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteCond %{THE_REQUEST} (.*)\.php  
      RewriteRule ^(.*)\.php $1.html [R=301,L]  
    
      RewriteCond %{THE_REQUEST} (.*)\.html  
      RewriteRule ^(.*)\.html $1.php [L]  
    
      # All else goes to the controller page
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^(.*)$ mycontroller.php [L,QSA]
    </IfModule>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to redirect using htaccess to a page that cchecks the query
I read somewhere that using .htaccess for 301 redirect is not recommended and that
How to redirect in url by using htaccess in php? http://www.example.com/randomstring/2 i would like
I want to redirect using .htaccess abc.com/test.php to abc.com/test . but it will execute/display
At the moment using .htaccess in my PHP Application to choose which page I'm
Possible Duplicate: How to add .php extension using .htaccess url rewriting? I want to
I have been using .htaccess files to redirect some of my renamed controllers/actions. Say
You can 'fake it' .php being a .html file using .htaccess: AddType application/x-httpd.php .html
I am using htaccess to redirect non-www to www like this: user typea: mydomain.com
I would like to forward users using .htaccess When they visit the page: http://www.example.com/add_event/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.