URL Extension - IIS server

Hello, I want to remove my extensions on my website
pages fro example .html. I saw that you need this in your
.htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

I have it in my root folder but its not removing it.

Try this:

   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 :frowning:
[/Note-to-self]]

ah ok I see thank you

So this is the correct code still right?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]

Not sure what he meant by:

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
1 Like

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.

Should I put the .htaccess file in my main root folder?
or is it fine where it is?

A could quick questions:

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?

Is there anything else in the htaccess file?

So this folder (website):

is here: (highlighted blue)

This is what I have in there:

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
With this line:
ErrorDocument 404 http://www.impactograph.com/404page.html
  1. move to last line
  2. removing line completely

####Edit:
Just tried on my own server and it had no effect :frowning:

JM hinted at the real problem here but everyone’s fallen short on the OP’s question.

  1. mod_rewrite will REDIRECT as instructed by its code.

  2. Removing a file extension will prevent the file from being displayed (without special treatment).

  3. The special treatment of #2 creates “loopy code” which must be handled carefully.

  4. The :fire: EVERYTHING (or nothing) :fire: 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.

Regards,

DK

There are no redirect flags in any of the code in this thread.

The “special treatment” is the not-a-file test, and it’s been there since the OP’s original post.

It actually doesn’t.

Personally I don’t recommend that tutorial. Too many flaws, too much bad information.

1 Like

[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 :-1: is the only one among the hundreds of :+1: 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!

DK

Except, of course, for responses #3 and #7.

I gave you specifics all the time, like this one for example. Just because you ignored them doesn’t mean I didn’t provide them.

I removed this and nothing happened, I get my
error message when I put extensioless URL…

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?

1 Like

ok ill check

I see the issue, my hosting is using windows not linux. Should the code
be different for web.config files?