Enable Mod Rewrite and. htaccess in apache

Source: Internet
Author: User
Tags manual writing

The Mode Rewrite module of Apache provides a Rewrite engine based on regular expression analyzer to Rewrite URL requests in real time.


Manager Introduction

Apache module mod_rewrite
Describes an engine for real-time URL rewriting requests based on certain rules.
Status Scaling (E)
Module name rewrite_module
Source File mod_rewrite.c
Compatibility is only available in Apache 1.3 and later versions.

Overview
This module provides a rewrite engine based on the regular expression analyzer to rewrite URL requests in real time. It supports a flexible and powerful URL operation mechanism for each complete rule with unlimited number of sub-rules and additional condition rules. This URL operation can depend on various tests, such as server variables, environment variables, HTTP headers, time tags, and even search databases that match URL components.

This module can operate all parts of the URL (including the path information) at the server level (httpd. conf) and directory-level (. and generate the final request string. The result of this rewrite operation can be internal sub-processing, external request redirection, or even internal proxy processing.

However, all these features and flexibility bring about a problem, that is, complexity. Therefore, do not expect to understand the entire module in one day.

For more discussions, details, and examples, see the detailed URL rewriting document.

 

In most cases, it works with the. htaccess file. For example, the URL (http://dancewithnet.com/2010/05/29/making-mod-rewrite-and-htaccess-work-on-mac-os-x) in this article is Wordpress with the Mod Rewrite module and the. htaccess file together, that is, the so-called Fixed Link (Permalinks ).


Windows
In Windows, we generally use the Administrator account, so enabling these two items is very simple:

1. Find # LoadModule rewrite_module modules/mod_rewrite.so in the [Apache installation directory]/conf/httpd. conf and remove the annotator #. If this row does not exist, add it. Check whether the mod_rewrite.so file exists in the modules folder under the apache installation directory. In this way, the Mod Rewrite function is enabled.
2. In the [Apache installation directory]/conf/httpd. conf, find

The Code is as follows: Copy code
<Directory/>
Options FollowSymLinks
AllowOverride None
Order deny, allow
Deny from all
</Directory>

Change "AllowOverride None" to "AllowOverride All", so that All folders are supported. htaccess or enabled for the specified folder. htaccess, which can be found in the [Apache installation directory]/conf/httpd. add in conf

The Code is as follows: Copy code
<Directory "D:/sites/example/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow, deny
Allow from all
</Directory>

This approach is generally configured with the virtual host, so most will write the above configuration code into the [Apache installation directory]/conf/extra/httpd-vhost.conf, which is clear and easy to manage.

3. After restarting Apache, it will be OK.
After the appeal step is completed, use the link settings except the default settings in the fixed link of Wordpress. Wordpress will generate the corresponding link in its installation directory directly. htaccess.

Mac OS X
In Mac OS X, the root account is generally not used, but the root permission is obtained through sudo. In general, we place website files in a personal directory, such ~ /Sites, which involves Mac OS permission management, which is much more complicated than Windows.

1. Run sudo vi/etc/apache2/httpd. conf on the terminal, find # LoadModule rewrite_module modules/mod_rewrite.so, and remove the annotator #.
2. Run sudo vi/etc/apache2/extra/httpd-vhost.conf, add

The Code is as follows: Copy code
<Directory "/Users/[user name]/Sites">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow, deny
Allow from all
</Directory>

So the whole ~ /Sites supports. htaccess.

3. Run sudo vi/Private/etc/apache2/users/[user name]. conf and change AllowOverride None to AllowOverride All. Note that in the previous Mac OS X version, the path may be/private/etc/httpd/users/[user name]. conf.
4. Create. htaccess in the desired directory and change the permission to 777. Here, we still use the fixed link of Wordpress as an example.
Cd ~ /Sites/Wordpress
Touch. htaccess
The default permission for chmod 777. htaccess to create a new file is 644. You can see it through ls-l. htaccess. At this time, the program cannot automatically write to. htaccess. This situation is safer, but manual writing is required.

5. restart Apache after exiting: sudo apachectl restart
After completing the preceding settings, you can use the Wordpress fixed link function. Note that if. htaccess is directly copied from Windows, an error may occur in the </IfModule> without matching <IfModule> section in the log. A simple solution is to create a file and copy and paste it again.

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.