3

I'm trying to structure my site to serve different areas of content, sort of like this one. I'm wondering how they make it so subdirectories have their own default pages, for example:

http://webmasters.stackexchange.com/questions/ask (the URL of the ask page).

It looks like a default page to me since it doesn't end in a file extension, but I don't know how that works. I'm running on an IIS server hosted on GoDaddy and I'm using the ASP.NET Web Pages (SPA) model if that makes a difference.

And if they're not default pages, why do they appear without file extensions?

  • You can do this in any web server, however, I do not know IIS anymore. I used to do this in an older version of IIS. It was a simple as right-clicking a folder in the IIS GUI and selecting "something" like properties... or whatever it is and giving it a file name. You may have to fish around a bit. But you can do this. It can be as simple as just putting an index.html in that directory. – closetnoc Nov 1 '15 at 23:29
  • You seem to be thinking that URLs always map to filesystem paths. However they can be (and often are) completely different. There might not be anything like /questions/ask on the physical filesystem. This might simply be a URL that maps to an entirely different resource. Simply by looking at the URL it is impossible to know exactly how it is implemented - there are too many possibilities. – MrWhite Nov 2 '15 at 2:15
2

It is very easy to configure a server to hide file extensions by default. On an Apache server for example, it can be done like this:

SOURCE

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

I assume IIS can be configured to do something similar, but I have never used IIS.

I'm not sure what you mean by "default page" though. If you want an index page to show up without a filename you just need to call it index.php, index.html, or something like that depending on your server setup.

For example http://www.example.com/ and http://www.example.com/index.html would be the same thing. When you go to the first URL, without the file name, it just shows you the index file anyway, but hides the file name from the address bar. If you are trying to hide the extension of a file besides the index file, you'll need some sort of re-write rule like above for IIS.

  • So is there an advantage to hiding the file extensions for security or something or why do some sites do it while others don't? – Sumba D. Imprtnt Nov 2 '15 at 2:52
  • The only security benefit would be that your site visitors would not know what programming language your site was written in. I think it just comes down to personal preference. – Sherwin Flight Nov 2 '15 at 3:16
  • 1
    Great answer. To complete it, let me add this. You also have "DirectoryIndex whatever_name_you_want.html" for htaccess, to change the default page to whatever_name_you_want file. It also allows you to set different files and prioritize, like this: "DirectoryIndex 1.html 2.html 3.html 4.html" (you could change extensions too). Finally, also common is to do "RewriteRule ^/?$ /custom-page.php [L,R=301]". – Giuseppe Nov 2 '15 at 7:00
  • @Giuseppe To mimic the DirectoryIndex, the R flag should be removed on the RewriteRule to make it an internal rewrite. – MrWhite Nov 2 '15 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.