Rewrite URLs in Apache using the Mod_rewrite module

Source: Internet
Author: User
Tags php write

If you have a third-party framework to do the project, the URL path can be the same way as the framework to set the method (such as thinkphp), but if the use of native PHP write projects and want to rewrite the URL, you can have some Apache settings to achieve the desired effect.

Before you change the configuration of Apache, you should be aware of some Apache knowledge: The role of the directory tag in Apache is to mark the behavior of how we customize Apache in a particular directory. The tag will have an absolute path, which is the directory where you want to customize the behavior.

In general, the idea of modifying URLs through Apache is to rewrite the URL addresses you want to rewrite in the. htaccess text. The steps are as follows:

(1) Now in the server's httpd.conf file, open the override. htaccess permission to add the following code to the httpd.conf file

<directory ". htaccess file Path" >

AllowOverride All

</Directory>

(2) where the. htaccess file is stored: If your previous access path is: Domain name/car_project/index.php

Obviously, this path accesses the index.php file in the Car_project folder, so if you want to rewrite the path, you should put the. htaccess file in the Car_project folder.

(3) then the. htaccess file will be opened with the rewrite engine inside, add the following code in the. htaccess file

<ifmodule mod_rewrite.c>

Rewriteengine on

</IfModule>

(4) Then we need to set the "redirect matching URL base address to apply" in the ifmodule tag, the instruction is rewritebase, the example is as follows:

rewritebase/demo/

And the address we're using now is: http://localhost/demo/

(5) After writing the rewriterule instruction in the ifmodule tag, the URL can be rewritten, for example by adding the following code to the tag:

Rewriterule somepage.php otherpage.php

This represents anyone accessing the somepage.php page, which is actually showing the otherpage.php page

Similarly, we can also use regular expressions to override the way the URL parameters are entered (in this case the regular expression is not unfolded in detail)

(6) So, now the httpd.conf file and the. htaccess file should have the following instance code:

In the httpd.conf file:

<directory ". htaccess file Absolute path" >

AllowOverride All

</Directory>

. htaccess file:

<ifmodule mod_rewrite.c>

Rewriteengine on

rewritebase/demo/

Rewriterule somepage.php otherpage.php

</IfModule>

Rewrite URLs in Apache using the Mod_rewrite module

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.