I am creating a webpage with PHP/MySQL, and each page is in its own .php file.
How should I organize the addressbar, when changing the viewing page?
I have thought of two different ways to solve this:
1) www.mywebpage.com/?page=REFERENCES+pageid=GUID_TO_USE_FOR_DB_LOOKUP
2) www.mywebpage.com/references.php?GUID_TO_USE_FOR_DB_LOOKUP
Which should I choose? Are there other ways to do this?
Edit: When the user clicks a link to another page, I want to redirect the user to the corresponding .php file. The question is how to show this in the addressbar in the web browser.
With the GUID_TO_USE_FOR_DB_LOOKUP, I want to pass parameter to the .php file
Another way to do it:
www.mywebpage.com/references/GUID.html
You can use mod_rewrite to use URLs like this and have them mapped to "references.php?GUID=xyz" internally.
However, all solutions are fine and will work the same way (display the page), the advantage of the above is just that the URL is more "readable" as well as better for search engine optimization as the URL-"path" is parsed by search engines (other than using parameters).
Search for "mod_rewrite SEO" to find out more about this topic, e.g.:http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/
A couple of questions to ask:
Why do you care about the URL? Should it be human readable? Do you care about SEO? Do you like to use a "framework" with a single point of entry (which could work like your first example)? Do you want to "hide" your actual filenames from your users? etc. etc.
Related
I want to mask some of the subdirectories and pages for my website. For example:
I want to change
www.example.com/blog.php?post=post1
to
www.example.com/blog
or something similar to that. I have seen examples about redirecting and such but that doesn't seem to work for what I want and I would like to stay away from iframes if possible. I just want the address bar to not reflect my internal directory structure and page names. I want it to keep showing /blog while they are moving from post to post. Thanks.
P.S. I am not using wordpress or any other CMS or blogging system.
You can use apache mod_rewrite for that.
Mod Rewrite Generator
And if You don't want to have the same url on blog-post/article change, but to display different content, all I can think of this moment is by using either POST method or browser cookies, but, that would require a lot of page reloads, and it simply is not recommended for wide use. If You are building per_se (one person only) panel or similar, than url doesn't matter that much, but, speaking of blog..
It is quite reasonable to hide .php extension or url query index/key, but not what You would like to accomplish.
Like I said, it is possible, but Luke .. don't do that.
Blog should be bookmarkable on each and every #stop, and that happens just because of unique urls and hash values. Without these two, no hyperlinking possible *(not to mention seo penals and flaws + dozen of other unwanted obstacles, page caching for instance).
I'm setting up a website where visitors will be greeted by a splash screen where they will choose a color scheme for the actual website; based on their selection, the actual website will load with a different stylesheet. I gather this can be done by concatenating a flag to the URL, then reading the flagged URL on the next page to determine the stylesheet to be loaded (for example, to load the dark theme, the url would become http://www.mywebsite.com/index-dark; clicking the light theme link would make the URL http://www.mywebsite.com/index-light. Problem is, I don't know how to add a flag to a URL, or how to read this flag on a different page. I've tried Googling the issue, but have found little practical information. How can this be done?
EDIT: I'd like to avoid using two separate pages, as I'll have multiple themes; that would mean copying basically every HTML page in my root multiple times, taking up space. I like the idea of a concealed $_GET variable, though.
Without more information, I can only give some general advice.
So I'm going to assume that you are building a page in PHP, so you could have two different urls and use mod_rewrite to convert /index-dark to /index?style=dark but that's crappy.
What you probably want is to use a cookie or a session. Basically you check a cookie, or session value, for the theme setting and then pick the appropriate CSS file when you generate the page.
This has several advantages:
Doesn't require using url rewriting, an error prone endeavour at the best of times
Allows for persistent setting (if you use a cookie) and doesn't involve complicated urls.
Allows for adding more themes without changing mountains of code, just add the setting to theme selector and the new CSS file.
GET variables are generally only useful for specific data sent with that request, a bit like POST variables are mostly for forms and submitted data. If you want persistent settings, then a session/cookie is the best option.
The "flags" you're mentioning are probably actually $_GET variables that have been disguised using mod_rewrite. What you can do is edit your .htaccess file to add in rewrite rules that change, say, www.mywebsite.com/index.php?style=index-dark to www.mywebsite.com/index-dark (unfortunately I don't have experience in how exactly to do this; I just know that it can be done) and have your PHP catch $_GET['style'].
So, currently I'm organizing my blog based on filename: To create a post I enter the name of the file. As opposed to storing the post in the database, I store them in PHP files. So each time I create a post, A new row in the table is created with the filename and a unique ID. To reference the post (e.g. for comments) I get the name of the current file, then search the entries table for a matching file name. The post ID of the comment matches the ID of that post.
Obviously this isn't the standard way of organizing a blog, but I do it this way for a few reasons:
Clean URL's (even cleaner than mod_rewrite can provide from what I've read)
I always have a hard copy of the post on my machine
Easier to remember the URL of a specific post (kind of a part of clean URL's)
Now I know that the standard way would be storing each post in the database. I know how to do this, but the clean URL's is the main problem. So now to my questions:
Is there anything WRONG with the way I'm doing it now, or could any problems arise from it in the future?
Can the same level of clean URL's that I can get now be achieved with mod_rewrite? If so, links are appreciated
I will be hosting this on a web host. Do only certain web-hosts provide access to the necessary files for mod_rewrite, or is it generally standard on all web-hosts?
Thanks so much guys!
P.S. To be clear, I don't plan on using a blogging engine.
As cletus said, this is similar to Movable Type. There's nothing inherently wrong with storing your data in files.
One thing that comes to mind is: how much are you storing in the files? Just the post content, or does each PHP file contain a copy of the entire design of the page as opposed to using a base template? How difficult would it be to change the design later on? This may or may not be a problem.
What exactly are you looking for in terms of clean URLs? Rewrite rules are quite powerful and flexible. By using mod_rewrite in conjunction with a main PHP file that answers all requests, you can pretty much have any URL format you want, including user-friendly URLs without obscure ID numbers or even file extensions.
Edit:
Here is how it would work with mod_rewrite and a main PHP file that processes requests:
Web server passes all requests (e.g., /my-post-title) to, say, index.php
index.php parses the request path ("my-post-title")
Look up "my-post-title" in the database's "slug" or "friendly name" (whatever you want to call it) column and locates the appropriate row that way
Retrieve the post from the database
Apply a template to the post data
Return the completed page to the client
This is essentially how systems like Drupal and WordPress work.
Also, regarding how Movable Type works, it's been a while since I've used it so I might be wrong, but I believe it stores all posts in the database. When you hit the publish button, it generates plain HTML files by pulling post data from the database and inserting it into a template. This is incredibly efficient when your site is under heavy load - there are no scripts running when a visitor opens up your website, and the server can keep up with heavy visitation when it only needs to serve up static files.
So obviously you've got a lot of options when figuring out how your solution should work. The one you proposed sounds fine, though you might want to give careful consideration to how you'll maintain a large number of posts in individual files, particularly if you want to change the design of the entire site later on. You might want to consider a templating engine like Smarty, and just store post data (no layout tags) in your individual files, for instance. Or just use some basic include() statements in your post files to suck in headers, footers, nav menus, etc.
What you're describing is kind of like how Movable Type works. The issues you'll need to cover are:
Syndication: RSS/Atom;
Sitemap: for Google;
Commenting; and
Tagging and filtering content.
It's not unreasonable not to use a database. If I were to do that I'd be using a templating engine like Smarty that does a better job of caching the results than PHP will out of the box.
My site currently handles URL's like this...
/?p=home
/?p=profile&userid=1
/?p=users.online
/?p=users.online&page=23
/?p=mail.inbox
/?p=mail.inbox&page=12
... and so on, there is probably at least 120-150 different pages, on my site a page is built like this,
index.php includes a main config file which then includes function/class files into it
index.php then includes a header file
index.php then includes the page file which is from the url ?p=pagename
index.php then includes a footer file
That is how every page on my site is compiled, through the index page like that and I have been considering/thinking about cleanning up the URL's as I am re-writing/restructuring most of my sites code right now, it's the perfect time to do it if I am going to do it. What I mean by cleanning up the URL's is using mod-rewrite so the URL structure above would look like this instead...
/home
/users/1 //user ID might switch to use username as well
/users/online
/users/online/23 or /users/online/page/23
/mail/inbox
/mail/inbox/12
So first of all is there any downfalls to doing this, does it create a lot more processing work since it is using mod_rewrite?
Also would it be hard to write the regex or whatever is needed to match the file names in the format that I show above, I listed only a few of the pages but there would be at least 100 different URL pages I have blogs, bulletins, forums, all kinds of stuff
Absolutely. That's one of Apache's main purposes, so it's been designed to do it very efficiently. In fact, I'd emplore you to use that method.
It makes cleaner URLs for visitors
It's fantastic for SEO
It makes the website more professional
It makes it easier for users to guess URLs if they are trying to find a certain page without actually traversing through your site ti find it.
There are no downfalls, and a ton of advantages to using that URL structure. Go for it!
I'm making an online dictionary. Now I have two options:
1) Use AJAX for retrieving results
2) Just use some regular PHP scripts
If I choose the first option more likely the online dictionary will have one page and it's fast. If I choose the second option I'll have more pages (separate page for each entry with it's own URL), but it's slower. Personally I like the second option, don't really like to much AJAX on pages.
What is your opinion? Cons and pros (for this certain case)?
Thank you.
If you use the second solution (ie, several URLs, one per page/definition), your users will be able to bookmark URLs for each specific page, use those to come back to youor site or link to it, or send them to other people, or whatever they want -- which is good.
If you only have one and only one page for your whole website, people cannot link to specific pages/definitions ; they have no way to come back to one specific word ; and that's bad :-(
Same for search engines, btw (Even if not that important for search results) : if they only see one page, they will not index your content well... and you probably want your site indexed.
So, in your case, I would probably go with several distinct URLs (even the corresponding pages are all generated by the same PHP script, of course).
And same thing for search results : you probably want to give people the ability to link to search results, don't you ?
About the "speed" thing : well, with Ajax, you'll send one request to the server. Without Ajax, you'll still send one request (for a bigger page, I admit), plus the ones for the images, CSS, JS, and all.
You should read a bit about Frontend optimization (see Yahoo's Exceptional Performance pages, for instance) ; it'll help quite a lot, about that ;-)
You could potentially use htaccess (assuming you're on a linux server) to give unique urls to each page which keeping one page for displaying content.
For instance:
http://yourdomain.com/s/sausage would point to http://yourdomain.com/page.php?id=426
Which then would pull from a db and show you the 426th result, which would be for sausage, hurray. You'd probably want to build a htaccess generator for this, or use a system of slugs and use regular expressions
htaccess would look like this:
$http://yourdomain.com/s/([^/]+)^ $http://yourdomain.com/page.php?slug=$1
this will send anything after http://yourdomain.com/s/ to page.php as a slug variable, then reference your database for that slug.
Not sure if that's in any way helpful.