Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: make page accessible without having to type .html

  1. #1
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default make page accessible without having to type .html

    hey guys so i have a couple handy pages but i want them to be accessible without the user having to type in the extension for the html file.

    so instead of:
    mywebsite.com/handything.html

    i want it to be seen and accessible as:
    mywebsite.com/handything

    i'm guessing there's something i need to put in my .htaccess file but i don't know exactly what. is there a line i can put in that will make it assume all files in the folder, with or without extensions, should be parsed as html and php? or maybe a line that automatically parses the requested url with an .html at the end, but without showing it on the url?

    i've made html pages work with php in them by putting in the line:

    AddHandler application/x-httpd-php .html .htm

    but now i want to take it a step further and have the page work without an extension in the url all together.

    anybody know?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    The easiest solution is to create a directory with that name then put your html file inside it and rename it to index.htm or index.html. That's the proper way to do it.

    You can do this using .htaccess, with mod_rewrite, but using mod_rewrite is complicated and I'd recommend avoiding it if you can.

    The other reason not to use mod_rewrite in this case is that it would either make it impossible to have directories on your site (every directory would be assumed to be an html file), or you could need very complicated rules for when you want to use a real directory and when you want to pretend that a .html file is a directory.

    Anyway, if you do want to use mod_rewrite, then just do a search for some introductory information and learn the basics of how it works-- in short, it sees the requested URL and then lies to the visitor by showing them a different page, based on the rules you provide. It's powerful, but also complicated (both in theory and code). You'll probably need some regex (regular expressions) to be able to use it well. You can either rewrite ALL directories to *.html, or you could create a separate rule for every single file like that. But if you're doing that, I don't see why you don't just create the directories manually since it won't save that much time.

    Once you get the basics of how it works, you can probably find an example somewhere of someone doing what you're looking for with .htaccess and mod_rewrite.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    baconDelta (12-28-2011)

  4. #3
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    ah thank you so much for pointing me in the right direction daniel!

    mod_rewrite is exactly what i was looking for.
    it is pretty complicated but not too hard with my dir structure since nothing is pulled from a database.

    in case anyone cares this is what i did and it worked for me.

    my structure goes website.com/directory/34234983.html

    since all i want to do is snip off the .html at the end i just put this in the htaccess file:

    RewriteEngine On
    RewriteRule ^([a-z]+)/([0-9]+)$ /$1/$2.html [L]

    this says that any request in a directory structured as a-z/0-9 should be received as the same thing but with an html at the end.

    so now
    mywebsite.com/handything
    does indeed take the user to
    mywebsite.com/handything.html
    without displaying it in the url. although if the user puts .html at the end, it also takes them to the same page. i'll have to study a bit further to not allow this. for better security i think it'd be best if the user didn't know if the file was a php or html or what have you.

    thanks again daniel!

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Glad it works. Look into the redirect settings for that. I think you can tell it to redirect them or not.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    gr i see other sites doing it without redirection. the url is the same, but a 404 page comes up when i try to do a .html or .php on their site. this would mean it's a RewriteRule i think.

    either way i've been rattling my brain but it's hard to make it work without cutting off the page from being viewed all together.
    Code:
    RewriteRule ^([a-z]+)\.html$ /404 [L]
    RewriteRule ^([a-z]+)$ /$1.html [L]
    that's what i have now and it makes the page completely unreachable, even when .html is not typed in. i assume the line to make the page 404 if the user types in .html should be on top, since the second line puts in the .html when they don't type it in. i'm really not sure, perhaps it should go underneath. any input dan?

    grah if anyone has any ideas let me know.

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    These rules apply in a loop; I haven't found a good way to stop visitors from accessing the pages directly. I don't think that's a problem though.
    If you try to redirect from A to B then from B to A, you will end up with a loop that doesn't lead anywhere, so that's not the solution.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    hm well i'll have a page for uploading pictures. but neither the pictures nor any of the form fields are stored in a database. the only things i store in a db are the ip of the user, the url of the uploaded pic, and a timestamp.

    since the url is generated for them, and none of the fields work with the db, i don't have to worry about mysql injection correct? i'm just trying to justify to myself why i don't have to do the extension coverup on the upload page.

    edit: sorry to bug you with noob questions dan, if we ever meet up i'm buying you lunch.

  9. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure what you're asking. If user input of any kind (including a URL address that they could change) is used in a mysql query, then you should escape it. Even escaping when you don't need to won't hurt anything.

    But, yes, mysql escaping only applies when you are actually using the information in a query. If you're just using it in PHP and there is no way that that info would ever end up in a query, then it's irrelevant.

    I'm not sure how that connects to the hidden ".html" but I don't see a problem with it, either.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #9
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hi, Daniel nice clarification, yes you are right. I had been trying the way mod-rewrite for a long time, and as I'm a pigheaded; really I was unable to sort that out perfectly. And then I compltely left that matter. By the way, I got a ray of hope again in your words, in the first sentence but I couldn't get that properly. You meant, suppose I want to show like "mydomain.com/faq" instead of "mydomain.com/faq.html", then I have to create a directory like "FAQ", and I have to put the the faq.html file inside it, and thereafter I have to rename it as index.html. Which one I have to rename the directory "FAQ" or the .html file? Would you please let me know in details? I'm really confused regarding this matter. Thank you for your helpful advise.

  11. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,000
    Thanks
    44
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    The html file.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •