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

  • SEARCH
  • Home
  • 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 7419665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:06:36+00:00 2026-05-29T08:06:36+00:00

here is my configuration: http://domain.com (obviously fictitious name…) hosted on a server running Apache

  • 0

here is my configuration:

  • http://domain.com (obviously fictitious name…) hosted on a server running Apache with mod_rewrite enabled
  • folder named “foo”: (located at http://domain.com/foo/ and in which I want to put my .htaccess file) containing only 3 types of files .php, .htm, .html
    • .php files (for the sake of the example I will refer to one of them: phpfile.php)
    • .htm files (for the sake of the example I will refer to one of them: htmfile.htm)
    • .html files (for the sake of the example I will refer to one of them: htmlfile.html)
  • within the foo folder, no file has an equivalent with another extension or without extension (ie eg neither phpfile.htm nor phpfile.html nor phpfile exist in foo, only php.file.php does exist)

Here is what I am trying to achieve:

  • when entering http://domain.com/foo/phpfile.php or http://domain.com/foo/htmfile.htm or http://domain.com/foo/htmlfile.html in my browser’s address bar and hitting “Enter”:
    • I get redirected with a 301 to http://domain.com/foo/phpfile or http://domain.com/foo/htmfile or http://domain.com/foo/htmlfile (depending on the file I’ve chosen), that is to say that those latters are the URLs now displayed in the address bar
  • when entering http://domain.com/foo/phpfile or http://domain.com/foo/htmfile or http://domain.com/foo/htmlfile in my browser’s address bar and hitting “Enter”:
    • I don’t get redirected, nothing changes in the address bar but instead the server just serves me the phpfile.php or the htmfile.htm or the htmlfile.html, depending on which one I requested

I have been trying hard on this, and sofar I’ve came with this content for my .htaccess file (located in the “foo” folder), which is unfortunately only working in the last of the two cases, in which I am interested (ie serving “phpfile.php” when I request “phpfile”, serving “htmfile.htm” when I request “htmfile” or serving “htmlfile.html” when I request “htmlfile”), and which is ignoring the 301 redirections:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /foo

# Redirect permanently the requests for existing .php files
# to extensionless files (non present on the server)
# so that phpfile.php gets redirected to phpfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.php$
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule (.*)\.php$ $1   [R=301,NC]

# Redirect permanently the requests for existing .htm(l) files
# to extensionless files (non present on the server)
# so that htmfile.htm gets redirected to htmfile
# so that htmlfile.html gets redirected to htmlfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html?$
RewriteCond %{REQUEST_FILENAME}\.html? -f   
RewriteRule (.*)\.html?$ $1 [R=301,NC]

# Matching requests for non existing extensionless files
# with their existing equivalent on the server
# so that domain.com/foo/phpfile will display
# the contents of domain.com/foo/phpfile.php,
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule (.*)$ $1.php    [L]

# so that domain.com/foo/htmlfile will display
# the contents of domain.com/foo/htmlfile.html,
RewriteCond %{REQUEST_FILENAME}\.html -f    
RewriteRule (.*)$ $1.html   [L]

# so that domain.com/foo/htmfile will display
# the contents of domain.com/foo/htmfile.htm,
RewriteCond %{REQUEST_FILENAME}\.htm -f 
RewriteRule (.*)$ $1.htm    [L]

Thank you in advance for any help/ advice.

  • 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-05-29T08:06:37+00:00Added an answer on May 29, 2026 at 8:06 am

    There’s a logic flaw in the first two rules in that it’s the php or html file that exists. The URI check is also in effect a duplicate of the rewrite rule pattern and !f implies !-d. You can also fold these into a single rule:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*?)\.(php|html?)$        $1   [R=301,NC]
    

    The last two are OK, but I’d swap the order if html requests are more common than php

    Why MultiViews doesn’t help

    Options +MultiViews implements a concept known as content negotiation, and in doing this Apache invokes a subquery to parse the filename root name. One of the things that it does is to scan the directory for known filename.extension combinations so in this case if xxx.php exists and your request is for xxx then it will substitute xxx.php and do an internal redirection, which then causes your first rule to fire, removing the .php extension and this causes the error that you see.

    So (i) you need to disable multiviews, and (ii) ditto subqueries; (iii) detect and prevent retry loops. This is one solution which will do what you want:

    Options +FollowSymLinks  -MultiViews
    RewriteEngine on
    RewriteBase /foo
    
    RewriteCond %{ENV:REDIRECT_END}  =1
    RewriteRule ^ - [L,NS]
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*?)\.(php|html?)$        $1   [R=301,NC,NS]
    
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule (.*)$ $1.html   [L,E=END:1,NS]
    
    RewriteCond %{REQUEST_FILENAME}\.htm -f
    RewriteRule (.*)$ $1.htm    [L,E=END:1,NS]
    
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule (.*)$ $1.php    [L,E=END:1,NS]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My hosting supports only web.config IIS configuration. I want to redirect my http://domain ->
I'm trying set up an execution account following the steps here: http://msdn.microsoft.com/en-us/library/ms156302.aspx I follow
I've enabled production mode logging in Play! and I'm using this configuration from here
Here's my nginx configuration:- user http; worker_processes 1; events { worker_connections 1024; } http
I am using git configuration mentioned here If I create a new tab and
I have a simple unit test case (extensive question here ) on a configuration
I'm new to spring controllers using annotated controllers. Here is my configuration Bean definition
Here's a standard scenario: if(string.IsNullOrEmpty(Configuration.AppSettings[foobar])) throw new SomeStandardException(Application not configured correctly, bozo.); The problem
Here is my situation: I have an application that use a configuration file. The
Here's a common, simple task: Read configuration settings from a configuration file, save the

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.