0

I am trying to configure my site with an htaccess file so that any request ending in .html will be rewritten so that the new URL is identical except that ".html" is not present at the end. This should work regardless of how many directories deep the request might be.

So for example, http://site.com/page1.html -> http://site.com/page1

http://site.com/dir1/dir2/page2.html -> http://site.com/dir1/dir2/page2

http://site.com/~bob/dir3/page3.html -> http://site.com/~bob/dir3/page3

I would prefer a solution where I don't need to hardcode the domain name, but it would be acceptable if necessary.

My current .htaccess file is a standard Zend Framework .htaccess file, and it is working correctly. I am aware that I will need to change the rewrite base if I move the site elsewhere.

SetEnv APPLICATION_ENV development RewriteEngine On #Send all requests for non-existant files to index.php RewriteBase /~rburk/refactor RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 
2
  • Are these HTML pages related to your zend framework? Commented Sep 17, 2013 at 16:41
  • I am converting an existing site to the Zend Framework. The new pages do not have .html extensions, while the existing pages do, so I want the old URLs to redirect to the new ones. Commented Sep 17, 2013 at 16:46

1 Answer 1

1

Try adding these rules to the htaccess file in your document root:

RewriteEngine On RewriteCond %{THE_REQUEST} \ /(.*)\.html($|\ ) RewriteRule ^ /%1 [L,R=301] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ /$1.html [L] 
Sign up to request clarification or add additional context in comments.

2 Comments

This does not appear to work. I am testing in an isolated directory with no other interfering .htaccess files. There are no errors, but the rewrite is not occurring. A request to file /new.html is not being rewritten to /new
@rburk I left out a | in the parentheses in the first condition

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.