flyba
网普版主
![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif)
积分 79
发贴 39
注册 2004-12-25
状态 离线
|
#1 How to mod_rewrite your site powered by php script
1. Create htaccess file
Simply create new file with a name .htaccess
Then write the following line in it:
Code:
RewriteEngine On
Then upload your file either to root directory of your site (if your php script is installed in root directory) or in subdirectory of your site e.g /forums (if your php script is installed in subdirectory).
2. Create mod_rewrite rule.
This is the easiest part of the mod_rewrite process or at least it takes less time.
- Choose which of the links you would like to make more SE friendly. for example with forums you want to mod_rewrite links to forums, posts and probably to user's profiles. With some classifieds scripts or cms you want to mod_rewrite links to profiles, ads, news, articles, polls, reviews and comments. With some photo gallery script you want to mod_rewrite links to albums, categories, top and latest uploads, pictures. One main trick here is - mod_rewrite what comes with much of uniqiue and important content. So i will illustrate this with phpbb.
For forums it gives the following link viewforum.php?f=2
- Mod_rewrite rules start with the following expression RewriteRule
So you start:
Code:
RewriteEngine On
RewriteRule
- then you add ^ (it signifies the start of an url)
Code:
RewriteEngine On
RewriteRule ^
- then you add expression to which you want to rewrite your link. For example, you want link to look like forum2.html (why do we keep number? it is very important in phpbb, each forum has its own number. As a rule numbers are very improtant in all php scripts as they give information to script on what content to bring)
Code:
RewriteEngine On
RewriteRule ^forum2.html
However, there can be unlimited amound of such numbers and we cannot create rewriterule for each of them. So we create a universal rule. In mod_rewrite syntax you do in the following way: ([0-9]*) for numbers, ([a-zA-Z]*) for letters and ([a-zA-Z0-9]*) for numbers and letters. This way mod_rewrite will keep value that script requires. Thus, we have:
Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html
- then you add your link viewforum.php?f=2
Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=2
In order to cover all links viewforum.php?f=Value mod_rewrite uses $n, where n is a consistent number of values.
Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1
- then you provide guidance on this rule. Here is the list of possible guidance options. I honestly not fully at this moment uderstand each of them so i use only one [L]:
...
So your code will look like this:
Code:
RewriteEngine On
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1 [L]
So you have created mod_rewrite rule. Next step is...
3. Modify your php script
!!!At this stage you are about to make changes to your php script files. So you need to back up all files that you will change first. Create backup folder on your computer and copy there all files that you will change.!!!
This is the hardest part as each php script is not alike, although most php scripts have similar structures of which i will tell in a moment. At this moment i can say that you need to look into code of your php script and find those parts that are responsible for links.
About Structures of php based scripts:
1. Links are in templates
Some scripts contain all patchs and links in template files, usually these ones are html or tpl files. However, this can be php files as well. Template files as a rule stored in folders called desing or templates.
2. Links are in source files
By source files i mean files in such folders as source, sc, includes, inc, etc. These could also be places where you can find files to modify links. Files that can contain links might be called fucntions.php, source.inc.php,, etc.
3. Links are in files that carry their names
This is the case with phpBB. If you want to mod_rewrite link viewforum.php?f=$1 then you need to look into the following php files:
- viewforum.php
- viewtopic.php
- index.php
4. Links are everywhere. This is very usual case with most of the php based scripts. This is the hardest part in modification. You have to literary go through all files to find the link.
How do you test this?
First tip. Open the file and do search for name of the link. For example if you want to modify link viewforum.php?f=$1 then look for viewforum.php
Second tip. When you found appropriate code identify the variable that is vital. For example for viewforum.php?f=$1 $1 is vital as it differes all other pages named viewforum.php.
Third tip. Rename the file to whatever you planned in your mod_rewrite rule, save changes and upload your page overwriting back to your server. Then refresh the page and see if link changed. If yes, you are in the right place, if know, start with tip#1
Fourth tip. After you found out that link has changed, press this link and see if it brings you to right page. If it does not that you need to check your .htaccess file rules. Mostlikely you gave your apache module mod_rewrite wrong rule to rewrite.
转载请注明:http://www.cnpop.net
|
|
2005-4-27 12:45 PM |
|
flyba
网普版主
![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif)
积分 79
发贴 39
注册 2004-12-25
状态 离线
|
#2
In this post i will create a list of common mod_rewrite mistakes or ommissions that i beleive will help you create better mod_rewrite code and will assist you in mod_rewrite of your site. Some of them are related to mod_rewrite code itself, others are related to php and still others to your site code. Here are first two:
1) images problem: When creating a mod_rewrite with a desire to have several folders. For example:
Code:
RewriteRule ^(.*)/(.*)/(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]
You face a problem of images not displaying and your styles are not showing correctly. Reason for this is that you create folder with subfolders, while path to images and css files might be relative in your files. For exaqmple
Code:
<img src=img/sun.gif border=0>
or
Code:
<css=css/styles.css>
There are two ways out of this situation:
a) You manually change relative path to absolute:
Code:
<img src=/img/sun.gif border=0>
or
Code:
<css=/css/styles.css>
b) or you do not create folders/subfolders structure. So instead make the following code for .htaccess file:
Code:
RewriteRule ^(.*)_(.*)_(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]
or
Code:
RewriteRule ^(.*)-(.*)-(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]
or simply
Code:
RewriteRule ^(.*)(.*)(.*).html$ index.php?id=$1&display=$2&cat=$3 [L]
2) sessions problem:
Some of the premade php scripts have sessions in URLs displaying. For example
Code:
index.php?photo=3&sessionid=20de08e9b9eb696a51e50b165ec9513f
These are the hardest to mod_rewrite for one simple reason. It requires addition php coding to remove such sessions at least for guests. (Actually, removing them for guests is the only thing required in terms of sef. The thing is that search engines, especially google will look into the page Code:
index.php?photo=3&sessionid=20de08e9b9eb696a51e50b165ec9513f
and page Code:
index.php?photo=3
as two different pages with the same content, this is pinalized as duplicate content).
The main way out of this problem is remove sessions for guests. For some scripts like phpBB there are instructions on how to do that, for other there are none. So you need spesialised assistance of php programmer to help you out. From personal experience i have to say that for most premade php scripts it is easy to remove, for others possible
If it is not possible to remove session then you need to at least modify your php script not to mess up the site. Make sure the session comes after the url - for example
Code:
index45.html&session=343247234823406320423
instead of
Code:
index45&session=343247234823406320423.html
i hope this helps you out. If you want to share your mistakes, tips feel free to post them here.
|
|
2005-5-27 09:09 PM |
|
网普科技
网普管理员
![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) 网普科技人民公仆
积分 3080
发贴 2863
注册 2004-6-26 来自 网普科技
状态 离线
|
|
2005-5-27 09:11 PM |
|
茱莉娅
THE BODY SHOP美容顾问
![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) ![](images/kll_dz4/star.gif) 茱莉娅美体小铺
积分 3080
发贴 2863
注册 2009-5-21
来自 茱莉娅美体小铺
状态 离线
|
|
2005-5-27 09:11 PM |
|
|