. Htaccess file Rewrite

Source: Internet
Author: User
When I first played a webpage, I wrote the structure of the static page http://centoscn.com/about.htm. Later, I want to change it to idea. Each time you add a new page, you need to open a folder. In fact, we use the structure on the left to deploy the server.

Just play Web pages, write static pages such as http://centoscn.com/about.htm structure.
Later I want to change to http://centoscn.com/about/
However, since you are not familiar with rewrite, you have made the following adjustments and created a bunch of folders manually:

This directory structure is quite troublesome to maintain. Each time you add a new page, you need to open a folder.
In fact, we use the structure on the left to deploy the service. if the server is slightly set, we can support the desired URL effect.

How to play rewrite

First, check whether your server supports. htAccess.
If this product is supported, you do not need to restart the server every time you write a rule to facilitate testing.

Redirect 301/test.htm http://centoscn.com/

If it succeeds, when you access xxx.com/test.htm, it will automatically jump to my blog.
If it fails, you need to enable. htaccess:

  1. Open \ conf \ httpd. conf and find#LoadModule rewrite_module, remove #, and load this module
  2. Open \ conf \ extra \ httpd-vhosts.conf to find AllowOverrideNone, Change None to All

After the environment is ready, you also need to read the rewrite interpretation of the special characters in "pattern matching:

* Represents the first 0 or more characters
+ Represents the first 1 or more characters
? Represents the first 0 or 1 character
^ Represents the start position of the string
^ Represents "not" in square brackets. for example, [^/] represents a non-slash.
$ Indicates the position at which the string ends.
. Represents any character (that is, a wildcard)
\ Cancel the special meanings of the following characters and convert them to original characters.
-- This part of understanding comes from the end of chapter 6 of SEO art.

Start. open rewrite of. htaccess.
RewriteEngine on

Replace xxx.htm with xxx/
RewriteRule ^ (.*)\/$/Define 1.htm [L]

Let's give another example to better understand. this is to replace the xxx. PHP file on the server with xxx.html.
RewriteRule ^ (.*)\. Html$/$ 1.php [L]

More methods:

Redirect all users accessing www.centoscn.com/xxx/ to centoscn.com/xxx/
RewriteCond % {HTTP_HOST} ^ www.centoscn.com [NC]
RewriteRule ^ (. *) $ http://centoscn.com/#1 [L, R = 301]

Code for enabling redirection for a specified directory:
RewriteBase/top/

Redirection with search parameters:
RewriteRule ^ search/(. + )? $/Index. php? S = $1 [QSA, L]

Slash at the end of the Directory:
RewriteRule ^ (. + [^/]) $/$1/[R]

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.