PHP implements url rewriting and. htaccess

Source: Internet
Author: User

. Htaccess is a complete file name with only a suffix), it is used for configuration files under the Apache server, when. put the htaccess file in a folder, which is only valid for files and folders in the folder. Through the. htaccess file, you can configure the server to implement many functions, such as error locating, password protection, IP rejection, and URL rewriting.

By default, Apache does not support. htaccess. You need to modify the configuration file httpd. conf of Apache to make. htaccess effective.

Configuration method:

Configuration:

1. Find the httpd. conf file under conf in the apache installation directory, and open the file to modify

LoadModule rewrite_module modules/mod_rewrite.so this line of code has a # sign in front of it and deletes #.

2. Find

<Directory/>

Options FollowSymLinks ExecCGI Indexes

AllowOverride None

Order deny, allow

Deny from all

Satisfy all

</Directory>

Change None to All. <Directory/>. There may be multiple nodes, which are related to the PHP path.

3. Restart the apache service.

Next, create the. htaccess file and write the configuration in it. When creating a file in Windows, the file cannot only have a suffix. You can use notepad or other tools to save it as the file name.

To rewrite a URL, the configuration file uses a regular expression to write a URL and map the sum of the regular PHP files. The common syntax is as follows:

RewriteEngine on // on is enabled, off is disabled

RewriteRule (%a-za-z%1,%%%-(%0-9%%1,%%%.html $ B. php? Action = $1 & id = $2

RewriteRule ([a-zA-Z1-9] {1,})/([a-zA-Z1-9] {1,}) $ a. php? Controller = $1 & action = $2

RewriteRule MyController/[a-zA-Z1-9] $ MyController. php? Action = $1

ErrorDocument 404/404 .txt

I found a file token on the Internet.

Product. php? Id = 12 to product-12.html

RewriteEngine on
RewriteRule ^ product-([0-9] +) \. html $ product. php? Id = $1

Rewriting product. php? Id = 12 to product/ipod-nano/12.html

RewriteEngine on
RewriteRule ^ product/([a-zA-Z0-9 _-] +)/([0-9] +) \. html $ product. php? Id = $2

Redirecting non www URL to www URL

RewriteEngine On

RewriteCond % {HTTP_HOST} ^ optimaxwebsolutions \. com $
RewriteRule (. *) http://www.optimaxwebsolutions.com/#1 [R = 301, L]

Rewriting yoursite.com/user.php? Username = xyz to yoursite.com/xyz

RewriteEngine On
RewriteRule ^ ([a-zA-Z0-9 _-] +) $ user. php? Username = $1
RewriteRule ^ ([a-zA-Z0-9 _-] +)/$ user. php? Username = $1

Redirecting the domain to a new subfolder of inside public_html.

RewriteEngine On
RewriteCond % {HTTP_HOST} ^ test \. com $ [OR]
RewriteCond % {HTTP_HOST} ^ www \. test \. com $
RewriteCond % {REQUEST_URI }! ^/New/
RewriteRule (. *)/new/$1

Example:

The content of the. htaccess file is as follows:

RewriteEngine on // on is enabled, off is disabled

RewriteRule ^ ([a-zA-Z1-9] {1,})/([a-zA-Z1-9] {1,}) $ a. php? Controller = $1 & action = $2

RewriteRule ^ ([a-zA-Z1-9] {1,})/([a-zA-Z1-9] {1,})/$ a. php? Controller = $1 & action = $2

Note:

Regular Expression, which matches Controller/Action or Controller/Action/strictly and maps to a. php

A. php content

<? Php

Echo "your controller:". $ _ GET ['controller']. "<br> ";

Echo "your action:". $ _ GET ['action'];

?>

Enter http: // localhost: 8080/Controller/Action/

Is parsed to http: // localhost: 8080/a. php? Controller = Controller & action = Action

The two URLs are equivalent.

Note: adding a query string after the ing url does not affect normal ing. For example, enter http: // localhost: 8080/Controller/Action /? Value = 100.

Reference:

Http://www.htaccess-guide.com/

Http://corz.org/serv/tricks/htaccess.php

Http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

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.