|
Implementing Search-Engine Friendly URLs under
Apache
This method of URL rewriting is meant for those running Apache. It is
easy to set up because the only server configuration changes to be
done are done in the .htaccess file.
First, a look at the URLs and where we are heading with it for this example:
Original > www.yoursite.com/article.php?cat=category&id=23
Re-written > www.yoursite.com/article/category/23/
You can see that the rewritten version appears as if it is working off of your server's directory structure. In actuality, you are simply passing variables to a script with "/"'s in between. Let's do it:
- Save your article.php file (from the frontend files, not the
backend!) as a new file called "article". Be sure that this new file
has no file extension at all.
- Open your .htaccess file or create one if you do not have one already.
In this file, add the following:
<FILES article>
ForceType application/x-httpd-php
</FILES>
(This is based on using the Apache version of PHP.
Your Forcetype may be differnent if PHP is compiled for CGI)
The word "article" corresponds to whatever you named your
article file in Step 1. This code forces Apache to read "article"
as a PHP file even though it has no PHP extension.
- Save or upload your .htaccess file into the directory containing your
front-end PHP files for Miraserver.
- Open your "article" file in a text editor. Insert the following into
the top of the file:
$url_array=explode("/",$REQUEST_URI); //BREAK UP THE URL PATH USING '/' as delimiter
$cat=$url_array[2]; //Category
$id=$url_array[3]; //Article ID #
$pagenumber=$url_array[4]; //Page #
This code parses the URL into an array, using "/" as the delimiter.
Then, it accesses the variables in the array by their element
ID's. This code is designed for front-end files in the root directory
of your site. If they are located in a sub-directory of your site,
you will need to advance the element numbers to coincide.
- Save your "article" file. Ensure that there is no php,txt or htm extension
on the file. Upload the file to your server into the same location as
all of your other Miraserver front-end PHP files.
- Test it. The URL is working in this format:
www.yoursite.com/article/ CATEGORY SHORTNAME / ARTICLE
ID # / PAGE NUMBER /
Page Number is optional. If it is not defined, it will default to page
one.
The same process can be done on the category pages for Miraserver. Save a new copy of your "index.php" file as "category" with no file extension. Then use $url_array[2] to be the shortname for the category. You do not need an article number or page number. When done successfully, this will result in a URL like:
www.yoursite.com/category/shortname/
rather than:
www.yoursite.com/index.php?topic=shortname
Performing Necessary Edits in Templates
A successful re-writing hack will also necessitate some code editing
in the other templates. This is because there are some file paths which
are entered directly into the templates. So, all references to article.php
and/or index.php (for category pages) will need to be changed to the correct
format. The following templates will need to be addressed (assuming the
default template set is in use):
| Template |
Look At: |
| forumpost_article |
The link to the article which would be posted into vBulletin needs
to be reformatted |
| article |
In the relatedarticles block, change the path to article.php. |
| article_dropdown |
If you use the inter-page dropdown, then the URL to the article
pages needs to be changed. |
| page |
If you have enabled this hack for categories, too, then you need
to modify the URL to index.php in the sections block. You
also need to edit the article URL in the archives_row block. |
| bio |
In the articles_row block, edit the URL to the articles
that are listed for an author on their bio page. |
| latestarticles |
Edit the article URL that shows up on "Latest Articles"
lists. |
| previews |
Edit the article URL to which preview images will be linked on your
homepage |
| related_articles |
Edit the RELATED_KEYS block so that related articles will
be properly linked |
| searchresults |
So that search results are linked properly, edit the WEBPAGERESULTS
block (if this hack was done for category pages) and the ARTICLERESULTS
block. |
| vbul_newarticles |
The link to the article which would be posted into vBulletin needs
to be reformatted |
|
Editing Paths in the PHP Code
Starting with 1.0 Beta3, you will notice that we are now using $contenttype
inside of the global.php file to determine the type of content and thereby
determine which query to run to determine the template set. In previous
Beta versions, you had to perform string matching on the URLs themselves,
making this hack a bit harder to implement, especially for users who don't
know PHP. Users of Beta2 or prior will need to edit the string matching
code in global.php to properly match the format of your new URLs. With
Beta3, you can quickly edit the format of the URLs with four variables
near the top of global.php. Simply leave the [cat] and [id] holders in
place and you can change the rest of the URL. These variables will then
be used throughout Miraserver to change any URLs which may be embedded
into the code.
Here's an example:
| Original code: |
$category_url = "index.php?page=[cat]";
$article_url = "article.php?cat=[cat]&id=[id]"; |
| Change to: |
$category_url = "/category/[cat]/";
$article_url = "/article/[cat]/[id]/"; |
|
Page numbers in multi-page articles are generated using the pagenav()
function in functions.php. By default, it adds "pagenumber=x"
to the end of whatever URL is fed to it. If you perform this hack, you
will need to edit this function so that it outputs correctly formatted
URLs. Like so:
| Original Code: |
Change to: |
| $prevlink = "
<a href=\"$address&pagenumber=$prevpage\" title=\"previous
page\">«</a>"; |
$prevlink = "
<a href=\"$address$prevpage\" title=\"previous page\">«</a>"; |
| $nextlink = "<a
href=\"$address&pagenumber=$nextpage\" title=\"next
page\">»</a>"; |
$nextlink = "<a
href=\"$address$nextpage\" title=\"next page\">»</a>"; |
| $firstlink = "
<a href=\"$address&pagenumber=$curpage\" title=\"first
page\">« First</a> ... "; |
$firstlink = "
<a href=\"$address$curpage\" title=\"first page\">«
First</a> ... "; |
| $lastlink = "...
<a href=\"$address&pagenumber=$curpage\" title=\"last
page\">Last »</a>"; |
$lastlink = "...
<a href=\"$address$curpage\" title=\"last page\">Last
»</a>"; |
| $pagenav .= "
<a href=\"$address&pagenumber=$curpage\">$curpage</a>
"; |
$pagenav .= "
<a href=\"$address$curpage\">$curpage</a> "; |
|
Conclusion
For technically minded individuals, this is a pretty simple hack. But,
if you cannot wrap your head around this, you can contact me at support@miraserver.com
directly and ask me questions. I will also do the entire hack for you
for a small $25 labor charge.
|