urlwrite伪静态(SAE、PHP、JSP)

在SAE里,直接配置config.yaml文件,文件可以配置的内容包含:

  • 目录默认页面
  • 自定义错误页面
  • 压缩
  • 页面重定向
  • 页面过期
  • 设置响应Header的Content-Type
appname: xlzx
version: 1
handle:
# 默认首页
- directoryindex: index.php
# 默认错误页面
- errordoc: 404 /404.html
# 伪静态
- rewrite: if(!is_dir() && !is_file() && path~"([a-zA-Z0-9]+).html$") goto "$1.php"
# 后台登陆强制使用https访问
- rewrite: if (%{REQ:X-Forwarded-Proto} != "https" && %{REQUEST_URI} ~ "admin/(.*)") goto "https://%{HTTP_HOST}%{REQUEST_URI}"

具体可以参考SAE AppConfig

在PHP里面需要配置rewirte功能,打开http.conf文件清除LoadModule rewrite_module modules/mod_rewrite.so前的#,找到FollowSymLinks,其后AllowOverride All

在web目录新建.htaccess文件

<IfModule mod_rewrite.c>
#开启功能
RewriteEngine on
#匹配条件(正则)
#这个是伪静态
RewriteRule ([a-zA-Z0-9]+).html$ $1.php
</IfModule>

在JSP中需要配置一个过滤器,此处以Spring为例

需要安装urlrewrite.jar包,在web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!-- For Pseudo static -->
    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>WARN</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Add static resource directory -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- For error page -->
    <error-page>
        <location>/error.jsp</location>
    </error-page>

</web-app>

在WEB-INFO目录新建urlwrite.xml文件,并配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
<rule>
<note>This if for UrlWrite.Example:Request index.html,but the true address is index.jsp.
This is not true static address.
</note>
<from>^/([a-zA-Z0-9]+).htm$</from>
<to>/$1.jsp</to>
</rule>
<rule>
<from>^/([a-zA-Z0-9]+).html$</from>
<to>/$1.jsp</to>
</rule>
</urlrewrite>
原文地址:https://www.cnblogs.com/Jsonlu/p/4841275.html