Apache的Mode Rewrite模块和.htaccess文件配合
Apache的Mode Rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求。在大多数情况下,它和.htaccess文件配合使用。比如本篇文章的URL(http://dancewithnet.com/2010/05/29/making-mod-rewrite-and-htaccess-work-on-mac-os-x)就是Wordpress配合Mod Rewrite模块和.htaccess文件一起实现的,即所谓的固定链接(Permalinks)。
Windows
在Windows下,我们一般使用的是Administrator账号,所以启用这两项非常简单:
- 在[Apache安装目录]/conf/httpd.conf中找到
<font face="NSimsun">#LoadModule rewrite_module modules/mod_rewrite.so</font>
,去掉前面的注释符号#。如果没有这行,请添加。并确认apache安装目录下的modules文件夹中是否有<font face="NSimsun">mod_rewrite.so</font>
这个文件。这样就启用了<font face="NSimsun">Mod Rewrite</font>
功能。 - 在
<font face="NSimsun">[Apache安装目录]/conf/httpd.conf</font>
中找到<code><code><span style="WIDTH: 341px; HEIGHT: 120px" class="code"><code><code><directory></directory><br> Options FollowSymLinks<br> AllowOverride None<br> Order deny,allow<br> Deny from all<br></code></code></span></code></code>
Copy after login把“
<font face="NSimsun">AllowOverride None</font>
”改成“<font face="NSimsun">AllowOverride All</font>
”,这样所有的文件夹都支持.htaccess了,或者针对指定的文件夹启用.htaccess,可以在<font face="NSimsun">[Apache安装目录]/conf/httpd.conf</font>
中加入<code><span style="WIDTH: 340px; HEIGHT: 120px" class="code"><code><directory><br> Options Indexes FollowSymLinks<br> AllowOverride All<br> Order allow,deny<br> Allow from all<br></directory></code></span></code>
Copy after login这种做法一般是和虚拟主机配置在一起的,所以大多数会把上述配置代码写入
<font face="NSimsun">[Apache安装目录]/conf/extra/httpd-vhost.conf</font>
中,这样比较清晰易于管理。 - 重启Apache后,就ok了。
完成上诉步骤后,在Wordpress的固定链接中使用除默认外的链接设置,Wordpress会直接在其安装目录中生成对应的.htaccess,这样就可以使用设置的链接形式了。
Mac OS X
在Mac OS X中一般不使用<font face="NSimsun">root</font>
账号,而是通过<font face="NSimsun">sudo</font>
来获得<font face="NSimsun">root</font>
相关权限。一般情况下,我们把网站文件放在个人目录下,比如<font face="NSimsun">~/Sites</font>
,这样就涉及到Mac OS的权限管理,相对于Windows来说复杂不少。
- 在终端运行sudo vi /etc/apache2/httpd.conf,找到
<font face="NSimsun">#LoadModule rewrite_module modules/mod_rewrite.so</font>
,去掉前面的注释符号#。 - 运行
<font face="NSimsun">sudo vi /etc/apache2/extra/httpd-vhost.conf</font>
,加入<code><code><span style="WIDTH: 354px; HEIGHT: 120px" class="code"><code><code><directory><br> Options Indexes FollowSymLinks MultiViews<br> AllowOverride All<br> Order allow,deny<br> Allow from all<br></directory></code></code></span></code></code>
Copy after login这样整个
<font face="NSimsun">~/Sites</font>
都可以支持<font face="NSimsun">.htaccess</font>
。 - 运行
<font face="NSimsun"> sudo vi /Private/etc/apache2/users/[用户名].conf</font>
,把其中的<font face="NSimsun">AllowOverride None</font>
改成<font face="NSimsun">AllowOverride All</font>
。需要注意的是,以前的Mac OS X版本,路径可能是<font face="NSimsun">/private/etc/httpd/users/[用户名].conf</font>
- 在需要的目录新建.htaccess,并修改其权限为777,此处依旧以使用Wordpress的固定链接为例。
<code><code><span style="WIDTH: 365px; HEIGHT: 66px" class="code"><code><code>cd ~/Sites/Wordpress<br>touch .htaccess<br>chmod 777 .htaccess</code></code></span></code></code>
Copy after login新建文件的权限默认是
<font face="NSimsun">644</font>
,通过<font face="NSimsun">ls -l .htaccess</font>
就可以看到,此时程序无法自动写入.htaccess,这种情况比较安全,但是需要手动写入。 - 退出后重启Apache:
<font face="NSimsun">sudo apachectl restart</font>
完成上述设置之后,就可以使用Wordpress的固定链接功能了。需要注意的是,如果.htaccess是从Windows下直接复制过来,日志中可能会出现<font face="NSimsun"> without matching <ifmodule> section</ifmodule></font>
的报错。简单的解决方案就是新建文件,重新复制粘贴。
原文:http://dancewithnet.com/2010/05/29/making-mod-rewrite-and-htaccess-work-on-mac-os-x/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.
