RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]
# I think the NC means NoCase which could be causing a problem
``
I think we also need to be clear that rewrite rules don’t “change” URLs per se. They enable the alternate, extensionless version of the URL. If you type the URL with extension into the address bar, it’ll still work. Rewrites make it so you can also type the extensionless URL into the address bar, and it’ll work too.
We might also need to check that the path being rewritten to is a real file, otherwise what would be a 404 becomes an infinite loop. “/notapage” isn’t a file, so rewrite to “/notapage.html”. “/notapage.html” isn’t a file, so rewrite to “/notapage.html.html”. And so on.
The script was slightly adapted from a working .htaccess file using a Php Framework. Your comment accounts for spurious problems I have sometimes experienced.
[Note-to-self]
Please help me to keep mouth shut until I know what I am talking about
[/Note-to-self]]
RewriteEngine On
# If the request is not for a real file
RewriteCond %{REQUEST_FILENAME} !-f
# But the request plus extension *is* a real file
RewriteCond %{REQUEST_FILENAME}.html -f
# Then rewrite
RewriteRule ^(.*)$ $1.html
I put that code above in my .htaccess file
and I tried writing my website like http://impactograph.com/index
but it did not work, this website is a subdomain of my root folder.
You said your website is a subdomain, but the URL you just showed (impactograph.com) doesn’t have a subdomain in there. Are you accessing the right place?
ErrorDocument 404 http://www.impactograph.com/404page.html
RewriteEngine On
# If the request is not for a real file
RewriteCond %{REQUEST_FILENAME} !-f
# But the request plus extension *is* a real file
RewriteCond %{REQUEST_FILENAME}.html -f
# Then rewrite
RewriteRule ^(.*)$ $1.html
JM hinted at the real problem here but everyone’s fallen short on the OP’s question.
mod_rewrite will REDIRECT as instructed by its code.
Removing a file extension will prevent the file from being displayed (without special treatment).
The special treatment of #2 creates “loopy code” which must be handled carefully.
The EVERYTHING (or nothing) atom used above ignores the simple fact that xyz (or xyz.html … or {blank}.html or {blank}.php) is hardly likely to be a file on the server, i.e., CHECK that the file exists (as an .html or .php file) BEFORE redirecting!
ALL of this has been discussed and available for years on the tutorial adapted to a SitePoint article but is available at http://dk.co.nz/seo - expand the section titled Redirect TO New Format as that has both a discussion AND a solution. The check code is immediately above in the Extensionless Links section … I’ll leave it as an exercise for you.
[quote=“Jeff_Mott, post:14, topic:209546”]
dklynn:
mod_rewrite will REDIRECT as instructed by its code.
There are no redirect flags in any of the code in this thread.[/quote]
Redirect Flags? That has nothing to do with the problem, only the solution I offered. Nothing else above responded to the OP’s question.
[quote=“Jeff_Mott, post:14, topic:209546”]
dklynn:
Removing a file extension will prevent the file from being displayed (without special treatment).
The “special treatment” is the not-a-file test, and it’s been there since the OP’s original post.[/quote]
The not-a-file tests above did not determine whether xyz.html (or fav.ico.html) existed. This was the major point missed in all the original responses (redirecting {garbage} to {garbage}.html does not help any website visitor).
[quote=“Jeff_Mott, post:14, topic:209546”]
dklynn:
The special treatment of #2 creates “loopy code” which must be handled carefully.
It actually doesn’t.[/quote]
It would if redirecting FROM .html to extensionless and then FROM extensionless BACK TO .html. That seems to have been the OP’s request. While a typical “noobie” desire, it can be accomplished but only with the “special handling” I discussed above and provided alternatives for in my tutorial (otherwise, the code will be “loopy”).
You’ve repeated that for more than a year (without any specifics). Fortunately, your is the only one among the hundreds of I have received over many years (and is also counter to the ratings on the SitePoint article). If you would e-mail me your specific criticism, I would be happy to consider any comments you have and clarify for you and/or update my tutorial … OFFLINE!
I copy-pasted your code and it worked exactly as expected for me. So I’m presuming the issue is somewhere in your server setup or paths. Can you check and confirm what the document root is for “impactograph.com”? Remember this is an ordinary domain, not a subdomain. Then also check and confirm what the full file path is for the htaccess file you’re editing?