Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to start creating my website but I have a few questions.

  • http://website.com/directory/webpage - in this kind of website, it does not display a .php or .HTML extension at the end. How to achieve this?

  • if I have several pages like contact.html, aboutme.html etc Should I just create separate html files, or should I do something in one main index.php file and use PHP or JavaScript to handle the content according to the URL. Which is a more appropriate workflow?

share|improve this question
    
I understood do something in one main index.php differently than @StephenOstermiller - it sounds to me as though you want a common header/footer/etc for those pages, but don't know how that could be done, so that part ended up phrased oddly. Is that it? –  Izkata Mar 6 '13 at 19:05
    
@Izkata Yes, I mean just change the content of one div instead of creating a new html file. Is this approach better or should I create new HTML files? –  Ankur Sharma Mar 6 '13 at 19:18
    
Simplest version to get started with (since it sounds like you've never done this before) would probably be something like having a header.html, and inside contact.html (assuming you can run PHP in it) <php include('header.html'); ?>. –  Izkata Mar 6 '13 at 19:35
    
@Izkata alright. Thats what I will do. Thanks –  Ankur Sharma Mar 6 '13 at 19:40

3 Answers 3

up vote 4 down vote accepted

There are several ways to serve files without an extension.


One of the easiest is to use the Multiviews option in your .htaccess. That option allows the /webpage.html document to be accessed through the /webpage URI. Then you can use a rewrite rule to make sure that the version with .html gets redirected to the version without the extension. Here is the .htaccess:

Options +MultiViews
RewriteEngine On
RewriteRule (.*)\.html $1 [R=301,L]

Another way of doing it is having your entire site be handled by a single script that prints the correct page based on a parameter, and then use .htaccess to rewrite everything into that script. Here is the .htaccess for that approach.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /thescript.php?s=$1 [L]

Many content management systems (CMS) such as Wordpress name your URLs for you, and have settings for the style of URL that you wish to use. In Wordpress this can be controlled under "Setting" -> "Permalinks" where there are several options, none of which use the .html extension. I also use the custom permalinks plugin that allows me to specifically choose the URL that I want for every page.


As for your question about the number of pages on your site, it is common practice to have separate pages for contact.html, about.html, privacy.html, and faq.html. I could see merging contact information with "about us" information, but I wouldn't try to put everything in one page, it gets too long.

share|improve this answer

It is not a good idea from an SEO point of view to serve different content via Javascript since a web-crawler is likely to either see an empty page or always the default content. In either case, your content will not get fully indexed.

Serving different content via PHP is doable and some entire sites are served by only a single file. Those sites usually have a content management as a back-end which works that way or not. If you are to write your own site, it would mostly likely simply make it harder to develop and maintain.

The serving-without-extension part of your question has already been answered with two good ways. There are other variants depending on how your side is organized but generally you use Apache's RewriteEngine.

share|improve this answer

The simplest way of achieving URLs without .html in them is often to make a directory called webpage and have a file called index.html in it. The file is actually

http://website.com/directory/webpage/index.html

but you can link to it as

http://website.com/directory/webpage

Apache (and most other systems) have a configuration setting for which file names should be treated as indexes for this purpose.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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