`
xinlanzero
  • 浏览: 250416 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Drupal .htaccess rewrite 规则分析

    博客分类:
  • PHP
 
阅读更多

Drupal 默认通过 apache 的 mod_rewrite 来实现 Clean URLs 功能,大家可以打开根目录下 .htaccess 看到关于 mod_rewrite 的规则,下面我们来分析一下 Drupal 的 rewrite 规则:

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

RewriteEngine on 后面的三段,是当站点同时绑定了泛域名 (如 example.com) 和 www. (如 www.example.com) 的时候用的,作用是让它们跳转到同一个域名上,[NC] 是说明判断是否匹配域名 (%{HTTP_HOST}) 时忽略大小写,[L,R=301] 说明是最后的一条 rewrite 规则,并且以 301 响应来强制重定向。

接下来两段是表明 rewrite 的目录,如果 Drupal 放在别名的子目录里面,可以通过设定 RewriteBase 来让 apache 正确 rewrite (很多时候我们并不需要这样做)。

最后一段是正式 (无 # 注释掉) 的 rewrite 规则,!-f 判断文件不存在,!-d 判断目录不存在,!=/favicon.ico 判断访问路径不是 favicon.ico (这里避免了当 favicon.ico 确实不存在于文件系统中而导致的跳转,通常这不是我们想要的),过滤后的 URI 都会通过 rewrite 变成 index.php?q=xxx 这样的形式,[L,QSA] 说明是最后一条 rewrite 规则,QSA 说明在后面追加原有的 GET 串。

下面是 Zend Framework 的 .htaccess 设置:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

这个是官方文档里提出的 .htaccess 的写法,我已经有几乎一年没有用 ZF 了,记得当年 1.3 版本时的写法很简单的,显然现在进步了不少,是从文件系统上判断,而不是仅仅的正则匹配了。先判断是非空文件 (-s) 或者是一个有效的连接 (-l) 或者是一个目录 (-d) 就执行第一个跳转,这里用了 - 和 [L] 来指明符合上面的都不跳转并退出 rewrite,而其他的 URI 也就会全部 rewrite 到 index.php 了。

然后是 CodeIgniter 的 .htaccess 设置:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

CI 只是简单的判断了 URI 是否不以表达式里面的字符串开头,如果符合则 rewrite 题到 index.php,就如当年的 ZF。

最后看看 CakePHP 的:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L] 
   RewriteRule    (.*) app/webroot/$1 [L] 
</IfModule>

Cake 就更低低了,只是 rewrite 到 webroot 目录的对应文件~~~

估计现在大家对众多框架的 rewrite 规则都有了解,ZF 的改进也已经明确的告诉我们怎样做了 :)

分享到:
评论

相关推荐

    mac 下 drupal .htaccess配置文件

    mac电脑在xampp环境下搭建drupal使用的.htaccess配置文件,下载后将...如果还是访问不了则编辑httpd.conf文件,去除LoadModule rewrite_module libexec/apache2/mod_rewrite.so前面的#;并将AllowOverride None改为All

    drupal 简洁链接解决方案.rar

    在实际操作过程中,可能会遇到一些特定问题,例如404错误或重定向循环,这通常可以通过调整`.htaccess`或`.web.config`文件中的Rewrite规则来解决。 此外,对于Drupal的简洁链接,还可以通过自定义节点类型、内容...

    Rewrite伪静态组件

    在httpd.conf文件中找到LoadModule rewrite_module modules/mod_rewrite.so行,确保其未被注释。 2. **创建或编辑.htaccess文件**:在网站根目录下创建或编辑.htaccess文件,这是放置Rewrite规则的地方。文件内容会...

    drupal 6.12

    the mod_rewrite module and the ability to use local .htaccess files. For Clean URLs support on IIS, see "Using Clean URLs with IIS" (http://drupal.org/node/3854) in the Drupal handbook. - Various ...

    Apache开启URL重写功能方法.docx

    - 您可以在`/etc/httpd`或`/etc/apache2`目录下的`httpd.conf`或`apache2.conf`文件中查找类似`LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so`或`LoadModule rewrite_module lib/apache2/modules/...

    Drupal 6.2开启简洁链接

    LoadModule rewrite_module modules/mod_rewrite.so #LoadModule cache_module modules/mod_cache.so 如果这一个模块是默认开启的话就不用管他了 在HTTPD.CONF文件中的AllowOverride 代碼 全部替换成 AllowOverride...

    Apache中使非伪静态url跳转到伪静态url的方法

    - **Drupal**: Drupal也提供了多种方式来启用伪静态URL,包括通过`.htaccess`配置。 - **Joomla**: Joomla同样支持伪静态URL功能,可以通过系统设置中的“SEO”选项来进行配置。 通过上述解析,我们不仅了解了如何...

    LNMP环境中WordPress程序伪静态解决方案.docx

    由于Nginx与Apache服务器不同,它不支持.htaccess文件,因此需要通过不同的方法来实现伪静态。以下是一个详细的解决方案,帮助你在LNMP环境下配置WordPress的伪静态规则。 首先,你需要确认Nginx环境中是否已经有了...

    php检测apache mod_rewrite模块是否安装的方法

    在Web开发中,Apache ...结合其他相关文章中的内容,如检测MySQL同步状态、验证字符串UTF8编码、分析文件编码和检查IIS对.htaccess的支持,这些PHP编程技巧可以帮助开发者更好地理解和优化他们的Web应用环境。

    apache伪静态与iis伪静态规则与配置区别介绍

    - Apache伪静态广泛应用于各种开源CMS系统,如WordPress、Drupal、Joomla等。 - IIS伪静态则常见于运行在Windows服务器上的网站,如ASP.NET、PHP等。 7. **转换方法**: - 由于规则的差异,从Apache到IIS或反之...

    url伪静态简单案例

    Nginx服务器则通过server block或location block中的rewrite规则来实现。 2. **框架或CMS内置支持**:很多PHP框架(如Laravel、CodeIgniter)和内容管理系统(如WordPress、Drupal)都有内置的URL重写机制,只需要...

    apache_2.2.22

    6. **URL重写**:通过mod_rewrite模块,Apache 2.2.22能实现复杂的URL重写规则,帮助实现SEO友好的URL结构,或者隐藏实际的服务器路径,提升网站的安全性。 7. **动态内容支持**:Apache能够与PHP、Perl、Python等...

    Apache服务器实用大全

    - 配置文件解析:介绍`httpd.conf`和`.htaccess`等配置文件的结构和常用配置选项,如DocumentRoot、ServerName、VirtualHosts等。 2. **模块管理** - Apache模块介绍:讲解如何加载和卸载Apache模块,如mod_...

    环境搭建Apache 2.4 + PHP 7.3 + MYSQL 8 .docx

    - 去掉 `LoadModule access_compat_module modules/mod_access_compat.so` 和 `LoadModule rewrite_module modules/mod_rewrite.so` 前的 `#` 号。 4. 启用 MPM(多进程模块)以提高性能,取消 `#Include conf/...

    php伪静态方法

    在.htaccess或nginx.conf配置文件中设置规则,将动态URL转换为静态形式,然后通过PHP读取相应的参数处理请求。 2. 文件系统模拟:在服务器上创建与动态URL对应的静态HTML文件,当用户访问时,首先检查是否存在这个...

    apache_2.2.25.zip

    例如,`mod_rewrite`模块用于URL重写,这对于实现SEO友好的动态网站或者进行路径隐藏非常有用;`mod_security`则可以增强服务器的安全性,防止SQL注入和跨站脚本攻击等;`mod_proxy`则支持代理服务,可以实现负载...

    PHP开发手册(pdg格式)

    3. **URL重写**:通过.htaccess文件或内置的mod_rewrite模块实现URL美化,提高用户体验。 **五、错误与调试** 1. **错误报告**:PHP允许开发者调整错误报告级别,控制在开发和生产环境下的错误显示。 2. **日志...

    ntti

    4. **URL重写**: 利用.htaccess文件和mod_rewrite模块实现友好的URL。 **PHP与数据库** 1. 数据库连接: PHP支持MySQLi和PDO等接口连接数据库,如`$conn = mysqli_connect("localhost", "username", "password", ...

Global site tag (gtag.js) - Google Analytics