a question that I hope does not sound crazy: On a multilingual site, imagine several actual files with contents in them which are currently reachable under their file names:
website.org/en/tomato.php
website.org/nl/tomato.php
website.org/de/tomato.php
These are all one and the same php file, the folder infront of it tells the php to fetch what content. Now this name is short and handy for my file managemens, but the file names are all very unsexy for search engines. So, what are the ways to make these flat files having more exotic names like:
website.org/en/tomato.php-fist-The-Forgotten-Red-Punch
website.org/nl/tomaat.php-vuist-De-Vergeten-Rode-Vuist
website.org/de/tomate.php-faust-Die-Fergessen-Rote-Faust
Currently, my php nderstands the folder /en/ or /nl/ preceding the filenames and puts the correct language into the tomato.php file.
It is my dream to have a per language file name which then somehow tells php/apache to make it work and fetch invisibly underneath the naming engine the correct file.
I am really not ready for a cms framework, I'm happy I can manage my flat files myself. What would be a creative way to have a links management where sothat I can tags my links above the water more exoticly, while keeping the short file names below water for myself?
Solutions/ideas/suggestions/code are all very welcome and treated as possible answers and are Much appreciated.
This is very common to handle with Apache and mod_rewrite ... there are a number of ways to do this - the most common way is to rewrite all files to index.php
In .htaccess:
RewriteRule .* index.php [L]
Then in your index.php check PATH_INFO and compare against your database for the associated real file.
A few tutorials here:
http://techie-buzz.com/how-to/create-seo-friendly-urls-using-mod-rewrite-and-php-part-1.html
http://articles.sitepoint.com/article/search-engine-friendly-urls
Related
So I have this blog where I have two php files one serves as an index to all my blog posts
www.adityasastry.in/view.php?cat=1
the cat value stands for category I have three categories, programming, embedded systems and rant.
I want to change this something like www.adityasastry.in/1 should translate to www.adityasastry.in/view.php?cat=1
and another in the same directory(I can move this to a different directory if I want) which lets me view a particular blog post.
www.adityasastry.in/viewer.php?cno=32
I have indexed each of my blog posts with a number.
Ideally I want this to translate to www.adityasastry.in/1/32 if 32 is a blog post belonging to 1 category.
Please don't ask me what I have done cause I am not even sure this could be accomplished with PHP alone on a shared host !! I just selected couple of tags that I think are relavant to this.
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708 - This is a good tutorial.
Basically-
Find the httpd.conf file for your server and uncomment the following line by removing the #:
# LoadModule rewrite_module modules/mod\_rewrite.so
Create a file called .htaccess (no name, extension .htaccess) and open it in your code editor of choice and enter the following:
RewriteEngine on
Which enables the rewrite engine obviously. From there you can make rewrite declarations by writing RewriteRule followed by a regular expression that matches the URL you are trying to catch, followed by the URL you want to be "redirected" to, using $1, $2, ... , $n etc to match the bracketed parts of the regex respective of the order they appear in it.
RewriteEngine on
RewriteRule ^/view/([0-9]+)/?$ /view.php?cat=$1
Save that file in the root of your website. Would take http://adityasastry.in/view/666 and "redirect" to http://adityasastry.in/view.php?cat=666. I would suggest reading the article I mentioned above as it covers this practice in far greater detail.
If I understand you correctly, you could put this in your htaccess file. (assumes Apache)
DirectoryIndex view.php
See this Q&A for reference
If your site automatically goes to index.php, then you might skip this and then in index.php load whatever page/file you want with a conditional include.
None of what I am saying is intended to dispute the re-write answers, just giving alternatives.
my site has now become sufficiently large for me to think it's necessary to convert the pages to php pages to help me update it in the future. The problem is: my site has a number of links to it on various websites across the web. Eg these links point to www.example.com/page1.html but the page is now going to be renamed www.example.com/page1.php
How would people get around this problem? Simply redirect the html page to the php page? Are there any alternatives? Does this have any implications for SEO?
Thanks
URL Rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
More directly to the point: http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
The least intrusive method is to simply have your webserver treat .html files as PHP files. That way your links can stay intact, and progressively replacing static .html pages with actual php-enabled pages can be done in an essentially transparent method to users.
Remember, there is no such thing as a "PHP script". There are only files that contain <?php ... ?> code blocks, which will get interpreted/executed when the containing file is passed through PHP.
Unless some of your html pages contain SAMPLES of php code that could be misinterpreted as actual code, then there shouldn't be any issues with making run through PHP.
As a minor side benefit, it wouldn't be immediately obvious that your site is running on PHP, as all the urls would say ".html". But then, that's security by obscurity and shouldn't be counted on to be anything in the way of real security.
You can do a 301 redirect (this works fine for SEO), or just rewrite the URLs so page1.html points to page1.php internally.
Both solutions can be done with the .htaccess file (assuming you are using apache as your webserver)
Maybe consider using a tool such as Dreamweaver to manage your website. That way you can easily rename pages and update the links with a few clicks.
:)
As answered by Marc B,
there is no such thing as a "PHP script". There are only files that contain code blocks, which will get interpreted/executed when the containing file is passed through PHP.
i would say that it's true and if you want to absolutely turn your html files into php files simply put <?php before your <DOCTYPE html!> line and then ?> after your </html> line save it and rename it as example.php if it is example.html
if you are windows8 or higher user then click on 'View' in file explorer and then check 'File name Extension'. Now you'll be able to see the extension example.html and many other files extensions like .jpg, .mp3 e.t.c...., This helps you to easily rename exactly like example.php but not example.php.html as .html will not be visible if you are not checked File name Extension.
I would suggest that you use CodeIgniter (kickass php framework).
You can maintain the existing site structure also, by making use of CodeIgniter's URL suffix option .
I have been working with PHP for a couple of years now but I don't consider myself any more than a moderate programmer.
While creating some websites (varying from presentation websites to some simple CMS'es), I have taken 2 approaches which I will discuss below. My question is simple: which one is better in terms of customization (it needs to be as customizable as possible) and speed (to load as fast as possible).
First choice
files: header.php , content1.php, content2.php , footer.php
idea: all the content files include the header at the beginning of the file and the footer at the end.
Second choice
files: index.php , content1.php, content2.php
idea: based on a GET variable or something similar, index.php includes the relevant php file
Thanks in advance for the answer!
I have a nifty framework that works equally well for small sites as it does large. The structure is pretty much the same in all instances and my directory structure looks as follows:
.htaccess
inc/
tpl/
css/
images/
js/
index.php
The job of index.php is to determine what file to load, where any programming logic is held in files in the inc directory, and templates are held in the tpl directory. For example, index.php could be as simple as this:
<?php
switch ($_GET['filename']) {
case 'news':
require 'inc/news.php'; // your news functions
include 'tpl/news.tpl.php'; // your news template
break;
case 'events':
require 'inc/events.php';
include 'tpl/events.tpl.php';
break;
case 'contact':
require 'inc/contact.php';
include 'tpl/contact.tpl.php';
break;
default:
if ($_GET['filename'] == '') {
include 'tpl/home.tpl.php';
} else {
header('HTTP/1.0 404 Not Found');
include 'tpl/page_not_found.tpl.php';
}
break;
}
Coupled with the following .htaccess rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?filename=$1
Hopefully that makes sense. If not, let me know what doesn’t and I’ll be happy to explain further.
I would go with the 2nd one. This is the approach taken by many frameworks and a very good one. You have a single place that get's always called, so you could do some processing in there like cleaning up the URL, checking for a valid Session,...
Furthermore, you can store some basic options inside the index.php (since it's a small site I see ne problem in this way of doing it but with bigger projects, sperate config files are the better way IMO) that can be accessed by all pages that are getting called (included) from within the index.php file.
Speed should not be an issue with a small site so whatever solution you may choose in the end will have good results.
i didnt use any framework for my projects. but everyone advice me to use a framework.
i maintain a file/directory structure of my own
INSIDE ROOT DIR
/backups
/config
/library
/media
/models
/modules
/templates
/sql files
index.php
If the website really is simple and doesn't need any additional utility, i would go with the second choice. Mostly the HTML is not so complicated, that it has to be splitted into several files, the actual presentation magic should rest in the CSS file anyway...
With the second choice you have your - simple - website in one place and don't have to look around in several files if you want to edit something.
Victorelu - not really an answer to your question. But you might be advised to begin looking at some of the lighter weight MVC php frameworks (i'm not talking about CI, kohana, cake etc, more the very lightweight ones such as caffeine). I did a little proof of concept using caffeine (a lightweight MVC framework based on a few core files) that neatly addresses the requirement that you state:
'... customization'.
jim
[edit] - link to caffeine: http://code.google.com/p/caffeine-php/
there's agreat little pdf that walks the entire process: http://code.google.com/p/caffeine-php/downloads/list
Check mofe information about website page structure.
The most important and very basic part of php website is page structure of php website because it help when you want to redesign website or you want to edit some code then sure you need to know page structure of website.
Check full detail here
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!
When I go to some websites I notice that there is no file extension on the page. Actually, this site is a proper example. Anybody know how this is done? :]
This is generally accomplished with URL Rewriting (link goes to a great introductory tutorial).
In the simplest case, a rewrite rule simply redirects every address by adding ".php" (or whatever other file extension) on the end of the url. So if someone tries to visit http://www.yoursite.com/about-me, they are actually viewing the content coming from the file about-me.php.
This could also be an MVC framework. (Search here, there are lots for php, a famous one for .NET, and I am sure many frameworks for many language.
This site has a root file, lets call it index.aspx (its not, but go with me) and that file has special code to read more of the URL. Used in combination with URL rewriting as mentioned, that special file takes in what would normally be path information and treats it as variable input.
Possible to remove extentions on php pages?
is something like
https://stackoverflow.com/index.aspx/questions/778799/possible-to-remove-extentions-on-php-pages
With index.aspx being the file that is hidden via URL rewriting.. Questions is a parameter indicating the controller, 778799 being a parameter that and the other text being the final parameter. (That is ignored)
For more see
http://www.asp.net/mvc/
http://codeigniter.com/
http://en.wikipedia.org/wiki/Model-view-controller
Aside from URL rewriting, you can also change apache's configuration to support alternative file extensions allowing you to do do something like MyPHPPage.html.
Regards,
Frank
Use mod_rewrite. Just put code like this in a file called .htaccess on your root:
RewriteEngine on
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?resource_type=$1&id=$2 [QSA,L]
Notice how $1 and $2 access the two quantities in parenthesized portions of the regular expression. It seems easy to extend the code from here