apache url rewrite实现url重写配置详解

apache url rewrite实现url重写配置详解

www.111cn.net 更新:2013-07-18 编辑:Crese 来源:转载
如果你是apache服务器要实现url rewrite重写我们需要在apache中打开mod_rewrite.so配置,下面我把具体过程给各位同学详细介绍介绍。

配置步骤:

第一步:找到apache的配置文件httpd.conf(文件在conf目录下)

第二步:你首先必须得让服务器支持mod_rewrite,如果你使用的是虚拟主机,请事先询问你的主机提供商。

打开httpd.conf,找到

 代码如下 复制代码

#LoadModule rewrite_module modules/mod_rewrite.so


把#去掉

找到AllowOverride None 改成 AllowOverride All,

注:AllowOverride 的参数设置为ALL,表示整台服务器上都支持URL规则重写。Apache 服务器要读每个网站下目录下的 .htaccess 文件。如果没有这个文件,或者这个文档没有定义任何关于URL重写的规则就不会有任何效果。

对于不同的网址,需要在APACHE中增加如下内容

 代码如下 复制代码

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9]+.eact.com.cn$
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
##RewriteRule ^u.eact.com.cn/(.*) /ux/index.html [L]
RewriteRule ^([a-z0-9]+).eact.com.cn/(.*) /$1/$2 [L]

实例

实例301跳转

例一.下面是在一个虚拟主机里定义的规则。功能是把client请求的主机前缀不是www.111cn.net和70.40.213.183都跳转到主机前缀为http://www.111cn.net,避免相同内容的网页有多个指向的域名,如http://111cn.net。

 代码如下 复制代码

NameVirtualHost 70.40.213.183:80
ServerAdmin [email protected]
DocumentRoot “/web”
ServerName 111cn.net

RewriteEngine on #打开rewirte功能
RewriteCond %{HTTP_HOST} !^www.111cn.net [NC] #声明Client请求的主机中前缀不是www.111cn.net,其中 [NC] 的意思是忽略大小写
RewriteCond %{HTTP_HOST} !^70.40.213.183 [NC] #声明Client请求的主机中前缀不是70.40.213.183,其中 [NC] 的意思是忽略大小写
RewriteCond %{HTTP_HOST} !^$ #声明Client请求的主机中前缀不为空
RewriteRule ^(.*) http://www.111cn.net/ [L] #含义是如果Client请求的主机中的前缀符合上述条件,则直接进行跳转到http://www.111cn.net/,[L]意味着立即停止重写操作,并不再应用其他重写规则。这里的.*是指匹配所有URL中不包含换行字符,()括号的功能是把所有的字符做一个标记,以便于后面的应用.就是引用前面里的 (.*)字符。

例二.将输入 en.111cn.net 的域名时跳转到www.111cn.net

RewriteEngine on
RewriteCond %{HTTP_HOST} ^en.111cn.net [NC]
RewriteRule ^(.*) http://www.111cn.net/ [L]


实现 伪静态在每个虚拟主机的地方设置Rewrite参数

 代码如下 复制代码

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "I:/Job/Java"
ServerName localhost
RewriteEngine on
RewriteRule /([0-9]+).html /test.jsp?id=$1 [PT]
RewriteRule /([0-9]+)_([0-9]+).html /content.jsp?id=$1&id2=$2 [PT]
</VirtualHost>

apache设置静态文件缓存方法介绍

www.111cn.net 更新:2013-07-06 编辑:Win2000 来源:转载
在apache中我们要实现缓存文件需要开户mod_expires模块,缓存功能实现之后我们可以提升服务器的性能(缩短服务的响应时间、减轻服务器负担、减少网络带宽使用量)这里就不一一介绍了。

为了减少客户端对服务端资源的请求,可以开启mod_expires.so模块

apache%C5%E4%D6%C3/" target="_blank">apache配置文件中去掉这段

“#LoadModule expires_module modules/mod_expires.so

”前面的#号


Httpd.conf配置

 代码如下 复制代码

ExpiresActive on

#缓存3天。

ExpiresBytype text/css "access plus 3 days

ExpiresByType application/x-javascript "access plus 3 days "

ExpiresByType image/jpeg "access plus 3 days "

Expiresbytype image/gif "access plus 3 days "


加入下面内容

 代码如下 复制代码
<IfModule mod_expires.c>
# mod_expires
ExpiresActive on
ExpiresDefault A864000
ExpiresBytype text/css “access plus 14 days
ExpiresByType text/javascript “access plus 14 days ”
ExpiresByType application/x-javascript “access plus 14 days ”
ExpiresByType application/x-shockwave-flash “access plus 14 days ”
ExpiresByType image/* “access plus 14 days ”
ExpiresByType text/html “access plus 14 days ”
<FilesMatch “.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|jpg|gif)$”>
ExpiresDefault A864000
</FilesMatch>
</IfModule>

默认缓存时间是10天,css缓存时间是14天


Apache搭建多个站点方法详解

www.111cn.net 更新:2013-06-28 编辑:Bolshevik 来源:转载
Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows iis直接点击几下就好了,下面我来给各位介绍配置方法。

最平常的大概有3种方法。

第一种:单IP不同端口

第二种:多IP同端口(独立IP的虚拟空间)

第三种:域名绑定根目录的方式(共享IP的虚拟空间)


Apache的核心配置文件名是”httpd.conf”,其所存放的路径在Apache目录下的conf文件夹下。修改它只需要使用记事本(建议使用其他编辑器,带行数的那种,方便修改),生效的话只需要保存httpd.conf,重启apache即可。

下面多站点支持的话,修改httpd.conf的第187~264行(不同的httpd.conf可能有差异),也就是在ServerAdmin和ServerName那里,大部分是注释。下面是主要修改的地方。

注意:如果是服务器请备份httpd.conf后再修改文件。

 代码如下 复制代码

# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition.  These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. [email protected]
#
ServerAdmin [email protected]

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride All
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "g:/www"
<Directory "g:/www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

第一种一般是测试环境,毕竟加了端口,如何绑定域名,访问的时候域名后面也需加端口。

例子分别通过80和8080访问不同的根目录。

大概在50几行有个Listen 80,在下面添加8080端口。

 代码如下 复制代码

Listen 80
Listen 8080<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options  Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName localhost:8080 
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

第二种多IP同端口。

IP地址1:192.168.2.2

IP地址2:192.168.1.68

端口同是80端口。

 代码如下 复制代码

<VirtualHost 192.168.1.68:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

第三种同IP不同域名和根目录(域名的话修改本地host演示)。

 代码如下 复制代码


<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName www.111cn.net
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName www.111cn.net
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>


猜你喜欢

转载自blog.csdn.net/csnewdn/article/details/53887996