HTACCESS Rewrite&重定向问题

时间:2011-06-14 16:43:51

标签: apache .htaccess mod-rewrite redirect

1)我想创建一个重写规则,如果文件扩展名是PHP或(S)HTM(L),则删除URL末尾的文件。例如:

This: http://www.example.com/index.php
Becomes: http://www.example.com/

This: http://www.example.com/index.php?action=login
Becomes: http://www.example.com/?action=login

2)我想用我的.htaccess设置一些URL重定向,这样如果有人浏览某个路径会自动重定向到/index.php。例如:

This: http://www.example.com/classes/(index.php)
Redirected To: http://www.example.com/(index.php)

This: http://www.example.com/classes/class.php
Redirected To: http://www.example.com/(index.php)

This: http://www.example.com/classes/style.css
Redirected To: http://www.example.com/(index.php)

有关它的任何线索? 非常感谢!

1 个答案:

答案 0 :(得分:0)

对于1)你可以反转你的问题,mod-rewrite的工作是读取脏网址并找到真实文件,所以你想要的是使用一个假URL(没有index.php)和那个mod-rewrite找到index.php。 Mod-rewrite不会修改您的网址,这是您的应用程序代码作业(PHP?)

因此,假设您的应用程序正在编写此URL:

http://www.example.com/

apache将使用

的事实
http://www.example.com/index.php

http://www.example.com/index.htm

http://www.example.com/index.html

响应不是由mod-rewrite处理,而是由Apache配置指令

处理
DirectoryIndex index.php,index.htm,index.phtml

对于http://www.example.com/?action=login,它也应该有用。

2):嗯,目前还不清楚。您是否一直想重定向尝试访问类目录的人?然后有两件事:

  1. 如果不能从Web访问此目录,将其放在Web目录之外,则没有人会看到它,如果它只包含php类,则PHp可以包含不在Web目录中的文件。如果它包含css和js文件,那么它是一个错误,在root中重定向它们肯定不会给它们一个js或一个css
  2. 你不需要mod-rewrite来制作simpel重定向,mod_alis,这是一个你肯定拥有的模块(99%的机会)会产生RedirectRedirectMatch个关键字,这样你可以写:

    RedirectMatch permanent /classes/.* http://www.example.com/

  3. 始终检查this page:,了解可以轻松完成的规则​​,而无需进入mod-rewrite的大脑卷积。