View Full Version : make page accessible without having to type .html
baconDelta
12-28-2011, 09:44 PM
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?
djr33
12-28-2011, 10:35 PM
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.
baconDelta
12-28-2011, 11:31 PM
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!
djr33
12-29-2011, 07:50 AM
Glad it works. Look into the redirect settings for that. I think you can tell it to redirect them or not.
baconDelta
12-29-2011, 09:40 PM
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.
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.
djr33
12-30-2011, 12:03 AM
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.
baconDelta
12-30-2011, 05:40 AM
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.
djr33
12-30-2011, 08:01 AM
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.
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.
jscheuer1
10-13-2012, 02:53 PM
The html file.
djr33
10-13-2012, 06:07 PM
yoursite.com/faq.html
yoursite.com/faq/
yoursite.com/faq/index.html
The first URL is one option. That's what is described in this discussion. There is no shortcut except with mod_rewrite and lots of wasted time-- very complicated, not worth it.
The second option is the same as the third option-- index.html is the default in the faq directory, so you can just use that. Then you get the default shortcut of using the directory instead of the index page.
From what you said, I think that's right. What's still unclear?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.