SiamWebCity.com Ѻҧ䫵úǧ
͡ ѧ   
˹á
web

web
Ѻ䫵
web
͹
web
Domain 299.
web
Դ
web
 
Ѻ䫵úǧ ѺѲ;प Ѻ Facebook iPad iPhone Android Ѻͺҧ䫵 䫵 䫵 䫵 : 鹵Դͷ 繼Ѵͷҹ : ҡͧͷ½ԡĵɰԨ
¡
ǡѺ
ͧ͡
ŧҹҹ
ǡ͹
Ѻ䫵
; Facebook iPhone
mlm ٻ
ʵԧ
ԡѧâ
͹䢡Ѻԡ
ҹѺ
Ӷ辺
Դ

պ
͡ҹ  ͡ҹ
VDO ͹ Dreamweaver  VDO ͹ Dreamweaver
Bitcoin Է¹  Bitcoin Է¹
PHP MySQL  VDO  ͧ  PHP MySQL VDO ͧ
PHP MySQL  紤  PHP MySQL 紤
jQuery  jQuery
HTML5  HTML5
ASP net   MS SQL  ASP net MS SQL
facebook app  facebook app
JSP  JSP
Oracle  Oracle
CSS  CSS
Google  Google
iphone ipad iphone itune ios  iphone ipad iphone itune ios
SEO  SEO
SQL Server  SQL Server
AJAX  AJAX
ôѵѵ  ʻԧ  ôѵѵ ʻԧ
   Թ   Թ
JavaScripts  JavaScripts
cloud computing  cloud computing
xhtml  xhtml
Photoshop  Photoshop
RSS Feed  RSS Feed
wordpress  wordpress
Computer  Computer
MLM µç §ҹ  MLM µç §ҹ
NUTT's blog  NUTT's blog
մԨԵ DVB T2  մԨԵ DVB T2
Bootstrap CSS  Bootstrap CSS

Ѻ͹ PHP, MySQL, HTML, CSS, Javascripts, FTP, Dreamweaver,PHPMyAdmin, Appserv, WMSEVER, php-Nuke , Joomla, SMF, Oscommerce, Phpbb, Paypal, Adwords¹ҧ纡ѹ
ԡ͹ й Adwords , PHP, MySQL, HTML, FTP, SMF, PHPMyAdmin, Dreamweaver, Oscommerce, Photoshop, Flash...

test speed
Display Pagerank
ʶԵ䫵

URL RewriteRule 1



SEO > URL RewriteRule 1


What do I need to get mod_rewrite working?

Theres pretty much only one thing youll need to get mod_rewrite working for you, and thats to have the mod_rewrite module installed on your Apache server!

For the purpose of this article, Im going to assume that you dont have access to view or edit the Apache server httpd.conf file, so the easiest way to check whether the mod_rewrite module is installed will be to look on your phpinfo page. If youve not already created one of these for yourself, just copy and paste the following code into an new text file using your favourite text editor, save it as phpinfo.php, and upload it to your server:

<?php phpinfo(); ?>

Load that page up in your web browser, and perform a search for mod_rewrite. All being well, youll find it in the Apache loaded modules section of the page. If it isnt there, youll have to contact your hosting company and politely ask them to add it to the Apache configuration.

Assuming the mod_rewrite module is loaded, then youre good to go!

A simple mod_rewrite example

So, lets write a simple mod_rewrite example. This isnt going to be anything fancy; were just going to redirect people who ask foralice.html to the page bob.html instead. First, lets create the Alice and Bob pages. Below is Alices webpage - create a similar one for Bob.

<html>
<head>
<title>Alice's webpage</title>
</head>
<body>
<p>          This is Alice's webpage       </p>
</body>
</html>

Upload both of these to your web server, and check that you can view both of them. Now comes the fun - were going to add a couple of lines to your .htaccess file. The .htaccess file is a text file which contains Apache directives. Any directives which you place in it will apply to the directory which the .htaccess file sits in, and any below it. To ours, were going to add the following:

RewriteEngine on RewriteRule ^alice.html$ bob.html

Upload this .htaccess file to the same directory as alice.html and bob.html, and reload Alices page. You should see Bobs page being displayed, but Alices URL. If you still see Alices page being displayed, then check youve followed the instructions correctly (you may have to clear your cache). If things still arent working for you, then contact your technical support people and ask them to enable mod_rewrite and the FileInfo override in their httpd.conf file for you

The structure of a RewriteRule

RewriteRule Pattern Substitution [OptionalFlags]

The general structure of a RewriteRule is fairly simple if you already understand regular expressions. This article isnt intended to be a tutorial about regular expressions though - there are already plenty of those available. RewriteRules are broken up as follows:

RewriteRule
This is just the name of the command.
Pattern
A regular expression which will be applied to the current URL. If any RewriteRules have already been performed on the requested URL, then that changed URL will be the current URL.
Substitution
Substitution occurs in the same way as it does in Perl, PHP, etc.You can include backreferences and server variable names (%{VARNAME}) in the substitution. Backreferences to this RewriteRule should be written as $N, whereas backreferences to the previous RewriteCond should be written as %N.

A special substitution is -. This substitution tells Apache to not perform any substitution. I personally find that this is useful when using the F or G flags (see below), but there are other uses as well.

OptionalFlags
This is the only part of the RewriteRule which isnt mandatory. Any flags which you use should be surrounded in square brackets, and comma separated. The flags which I find to be most useful are:

  • F - Forbidden. The user will receive a 403 error.
  • L - Last Rule. No more rules will be proccessed if this one was successful.
  • R[=code] - Redirect. The users web browser will be visibly redirected to the substituted URL. If you use this flag, youmust prefix the substitution withhttp://www.somesite.com/, thus making it into a true URL. If no code is given, then a HTTP reponse of 302 (temporarily moved) is sent.

A full list of flags is given in the Apache mod_rewrite manual.

A slightly more complicated mod_rewrite example

Lets try a slightly more meaty example now. Suppose you have a web page which takes a parameter. This parameter tells the page how to be displayed, and what content to pull into it. Humans dont tend to like remembering the additional syntax of query strings for URLs, and neither do search engines. Both sets of people seem to much prefer a straight URL, with no extra bits tacked onto the end.

In our example, youve created a main index page with takes a pageparameter. So, a link like index.php?page=software would take you to a software page, while a link to index.php?page=interestswould take you to an interests page. What well do with mod_rewrite is to silently redirect users from page/software/ to index.php?page=software etc.

The following is what needs to go into your .htaccess file to accomplish that:

RewriteEngine on RewriteRule ^page/([^/.]+)/?$ index.php?page=$1 [L]

Lets walk through that RewriteRule, and work out exactly whats going on:

^page/
Sees whether the requested page starts with page/. If it doesnt, this rule will be ignored.
([^/.]+)
Here, the enclosing brackets signify that anything that is matched will be remembered by the RewriteRule. Inside the brackets, it says Id like one or more characters that arent a forward slash or a period, please. Whatever is found here will be captured and remembered.
/?$
Makes sure that the only thing that is found after what was just matched is a possible forward slash, and nothing else. If anything else is found, then this RewriteRule will be ignored.
index.php?page=$1
The actual page which will be loaded by Apache. $1 is magically replaced with the text which was captured previously.
[L]
Tells Apache to not process any more RewriteRules if this one was successful.

Lets write a quick page to test that this is working. The following test script will simply echo the name of the page you asked for to the screen, so that you can check that the RewriteRule is working.

<html>
<head>
<title>Second mod_rewrite example</title>
</head>
<body>       <p>          The requested page was:
<?php echo $_GET['page']; ?>       </p>
</body>
</html>

Again, upload both the index.php page, and the .htaccess file to the same directory. Then, test it! If you put the page inhttp://www.somesite.com/mime_test/, then try requestinghttp://www.somesite.com/mime_test/page/software. The URL in your browser window will show the name of the page which you requested, but the content of the page will be created by theindex.php script! This technique can obviously be extended to pass multiple query strings to a page - all youre limited by is your imagination.

Conditional Statements and mod_rewrite

But what happens when you start getting people hotlinking to your images (or other files)? Hot linking is the act of including an image, media file, etc from someone elses server in one of your own pages as if it were your own. Obviously, as a webmaster, there are plenty of times when you dont want people doing that. Youll almost certainly have seen examples where someone has linked to one image on a website, only for a completely different, nasty one to be shown instead. So, how is this done?

Its pretty simple really. All it takes are a couple of RewriteCond statements in your .htaccess file.

RewriteCond statements are as they sound - conditional statements for RewriteRules. The basic format for a RewriteCond is RewriteCond test_string cond_pattern. For our purpose, we will set the test_string to be the HTTP_REFERER. If the test string is neither empty nor our own server, then we will serve an alternative (low bandwidth) image, which tells the person who is hotlinking off for stealing our bandwidth.

Heres how we do that:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?somesite.com/.*$ [NC]
RewriteRule .(gif|jpg|png)$ http://www.somesite.com/nasty.gif [R,L]

Here, the RewriteRule will only be performed if all the preceeding RewriteConds are fulfilled. In the second RewriteCond, [NC] simply means No Case, so it doesnt matter whether the domain name was written in upper case, lower case or a mixture of the two. So, any requests for gif, jpg or png files from referers other than somesite.com will result in your nasty image being shown instead.

The [R,L] in the RewriteRule simply means Redirect, Last. So, the RewriteRule will visibly redirect output to nasty.gif and no more RewriteRules will be performed on this URL.

If you simply dont want the hot linkers to see any image at all when they hot link to your images, then simply change the final line toRewriteRule .(gif|jpg|png)$ - [F]. The - means dont rewrite the requested URL, and the [F] means Forbidden. So, the hot linker will get a 403 Forbidden message, and you dont end up wasting your bandwidth.

Conclusion

mod_rewrite is an incredibly handy tool to have in your arsenal. This article only scratched the surface of what is possible with mod_rewrite, but should have given you enough information to go out and start mod_rewriting history yourself!



Short URL click! Facebook Share
<< Back : Ҫ 5,397 : 仴ҹ
Ѻ Ѻҧ纺 Ѻ䫴 webboard Seeding Ѻҧʵ 䫵 webUB.com



Ѻ䫵, , ҧ䫵, Ѻ, Ѻ͡Ẻ, Ѻ¹, ͡Ẻ, 纴䫹, Ѻҧ, ¹䫵, , ѷ , ѷ ҧ, ѷ ͡Ẻ, ѷ ¹, ŧҹ, ʵ,Design,Web,, ʵ,,ʵ ҤҶ١,䫵,,ʵ,Web Download,Web PHP,Flash MX,Graphic Design,Search Engine, Web, Design, Web Host,Web Hosting,Web Page,¹,¹䫵,Ѵ,Ѵ䫵,,䫵, , ,Ѻ¹,Ѻ,Ѻ䫵,Ѻ͡Ẻ,Ѻ͡Ẻ , ٻ,纴䫹,䫵, ҧ,ҧ䫵,͡Ẻ 䫵,͡Ẻ,͡Ẻ䫵,¹,ԡʵ,Ъ ѹ,ѺѴ䫵,Ѻҧ,,, ԡèѴ䫵,ѷѺѴ䫵,ԡèѴ䫵, ջʺóèѴ䫵, ҹԡ䫵,ŧҹ䫵,ŧҹ䫵,䫵,䫵 Ѫ "§",䫵ҤҷصԸ,䫵кҵðҹ,䫵ءҹ, 䫵áԨ, ,Ъѹ,,͡Ẻ,¹,ҧ,͡ Ẻ䫵,,,¹䫵,䫵,Ѻ,Ѻ͡Ẻ ,¹,Ѻ¹,Ѻ䫵,ѺѴ䫵,ʵ, Ѵ,,䫵,纴䫹,Ѻ͡Ẻ䫵,ҧ ,,,ԡʵ,Ѵ䫵,

Ѻ䫵 : : Ѻ¹ : ͹ҧ : Domain name 299. : Hosting : ԡô䫵 : Ѻ Rolex : Sitemap.xml
ʻԧ

ͺ ѧͺ кͺ ͺ  Exam.in.th
䫵͢: SiamWebCity.com webUB.com EXAM.in.th ͺ. 觺ҹ homeEST.com
Copyright © 2009-2019 SiamWebCity.COM. ® All rights reserved. Ţ¹СͺҹԪŤ͹ԡ : 0447314800321
E-mail, Google Talk : admin [at] siamwebcity.com, Skype: SiamWebCity.com
仴ҹ