Implementation of URL rewrite technology in Apache

Source: Internet
Author: User

Recently our company's website is in the optimization action, to adopt a better technology, so that the original PHP dynamic form of the Web page files can be Google and other search engines more easily revenue, we have adopted a number of improvements: for example, in the background with PHP program to change the original file to HTM content , using Apache Path_info technology, but feel still not strong enough, so decided to use Apache URL rewrite technology to try.

Very early attention to it, but has not known how to achieve, today bite teeth, turned the n article about the article, and finally realized some basic functions:

1. Modify Http.conf

In the root directory where you want to modify the site:


DefaultType application/x-httpd-php
Rewriteengine on #必须
Options FollowSymLinks #必须
rewriterule/news/(/d+)/.html/news/.php/?id=$1 [n,l] #规则
AllowOverride All

2. Modify Http.conf First


DefaultType application/x-httpd-php
Options FollowSymLinks
AllowOverride All

Then create a. htacess file in this directory, which reads as follows:
Rewriteengine on
rewritebase/websamples/urlrewrite/
Rewriterule news/(/d+)/.html news/.php/?id=$1 [n,l]

This enables the ability to parse http://localhost/news/1000.html into http://localhost/news.php?id=1000

Appendix: (A. htacess file for a Web site)

-------------------------------------------------------------------------------

ErrorDocument 401/error.php
Rewriteengine on
rewritebase/websamples/urlrewrite/power-for-less.com/

#show category
#
Rewriterule ^ (laptop ([^/]*)) | ( Camcorder ([^/]*)] | (((digital)?). Camera ([^/]*)] | (Power ([^/]*)) | (PDA ([^/]*)) | (Mobile ([^/]*)) | (Two-way ([^/]*)) | (Scanner ([^/]*)) | (MP3 ([^/]*)) /.htm (l?) $ babrand/.php/?fclassname=$1 [NC]

#show Brands
#
Rewriterule ^ (laptop ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (Camcorder ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (((digital)?). Camera ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$5 [NC]
Rewriterule ^ (Power ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (PDA ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (Mobile ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (Two-way ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (Scanner ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]
Rewriterule ^ (mp3 ([^/]*))/([^/]+)/.htm (l?) $ series/.php/?fclassname=$1&fb_babrand=$3 [NC]

#show Products
#
Rewriterule ^ (laptop ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (Camcorder ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (((digital)?). Camera ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$6 [NC]
Rewriterule ^ (Power ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (PDA ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (Mobile ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (Two-way ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (Scanner ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]
Rewriterule ^ (mp3 ([^/]*))/([^/]+)/(. +)/.htm (l?) $ product/.php/?fclassname=$1&fb_babrand=$3&code=$4 [NC]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.