根据使用htaccess文件的URL路由,重定向到特定页面

时间:2011-02-13 09:54:58

标签: .htaccess mod-rewrite

我有像

这样的网址
http://localhost/mis  
http://localhost/mis/newsletter.php  
http://localhost/mis/cms.php?mainid=65  
http://localhost/mis/news.php?mainid=93

我正在使用这些网址学校部分。

现在,对于 A-level 部分,我有以下网址

http://localhost/mis/index.php?mode=0  
http://localhost/mis/newsletter.php?mode=0  
http://localhost/mis/cms.php?mainid=65&mode=0  
http://localhost/mis/news.php?mainid=93&mode=0

我希望上面的网址像

一样路由
http://localhost/mis/a-level  
http://localhost/mis/a-level/newsletter.php  
http://localhost/mis/a-level/cms.php?mainid=65  
http://localhost/mis/a-level/news.php?mainid=93

请通过.htaccess文件帮我解决这些网址。

编辑: htaccess文件位于错误文件夹

文件结构:

root>>
    mis>>
        .htaccess
        newsletter.php
        cms.php
        news.php

6 个答案:

答案 0 :(得分:5)

/mis文件夹中的 .htaccess 文件中尝试这些。

# turning rewrite on
RewriteEngine On

# this will redirect: mis/a-level
RewriteRule ^a-level$ index.php?mode=0 [L]

# this will redirect all the rest
RewriteRule ^a-level/(.+)$ $1?mode=0 [QSA,L]

我认为像http://localhost/mis/cms.php?mainid=65&mode=0这样的行只是复制粘贴错误,您可以:http://localhost/mis/cms.php?mainid=65&mode=0

我测试了这些规则。如果它们不能在您的系统上运行,则必须存在一些配置问题。您的文件中是否还有其他重写规则?我不是,尝试启用重写日志记录以获取更多信息。必须在 httpd.conf 中启用此功能( .htaccess 中不允许使用它们。)

RewriteLog D:\bence\wamp\logs\rewrite.log
RewriteLogLevel 3

我的网址http://proba.test/mis/a-level/news.php?some=thing&other=33

的重写日志
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add path info postfix: D:/bence/www/proba.test/mis/a-level -> D:/bence/www/proba.test/mis/a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level/news.php -> a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'a-level/news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add path info postfix: D:/bence/www/proba.test/mis/a-level -> D:/bence/www/proba.test/mis/a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level/news.php -> a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'a-level/news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (2) [perdir D:/bence/www/proba.test/mis/] rewrite 'a-level/news.php' -> 'news.php?mode=0'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) split uri=news.php?mode=0 -> uri=news.php, args=mode=0&some=thing&other=33
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add per-dir prefix: news.php -> D:/bence/www/proba.test/mis/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (2) [perdir D:/bence/www/proba.test/mis/] strip document_root prefix: D:/bence/www/proba.test/mis/news.php -> /mis/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (1) [perdir D:/bence/www/proba.test/mis/] internal redirect with /mis/news.php [INTERNAL REDIRECT]
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/news.php -> news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/news.php -> news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (1) [perdir D:/bence/www/proba.test/mis/] pass through D:/bence/www/proba.test/mis/news.php

日志:http://proba.test/mis/a-level

127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level -> a-level
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'a-level'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (2) [perdir D:/bence/www/proba.test/mis/] rewrite 'a-level' -> 'index.php?mode=0'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) split uri=index.php?mode=0 -> uri=index.php, args=mode=0
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] add per-dir prefix: index.php -> D:/bence/www/proba.test/mis/index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (2) [perdir D:/bence/www/proba.test/mis/] strip document_root prefix: D:/bence/www/proba.test/mis/index.php -> /mis/index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (1) [perdir D:/bence/www/proba.test/mis/] internal redirect with /mis/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/index.php -> index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'index.php'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/index.php -> index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'index.php'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (1) [perdir D:/bence/www/proba.test/mis/] pass through D:/bence/www/proba.test/mis/index.php

答案 1 :(得分:0)

得到了......

RewriteCond %{QUERY_STRING} ^(.*)mode(.*)$
RewriteRule mis/(.*) /mis/a-level/$1?%1 [R]

架构供参考:

/root/
    .htaccess
    /mis/
        /a-level/
            newsletter.php
            cms.php
            news.php

localhost/mis/newsletter.php?mainid=65&mode=0

现在重定向到

localhost/mis/a-level/cms.php?mainid=65 - - (MODE BE GONE): - )

param / value剥离不需要php。非常巧妙的技巧,如果你可以控制你的Cond /规则和RegEx。 (注意:几乎相同的Cond / Rule可能需要直接在此解决方案之前捕获index.php文件)

您实际上也可以更改参数/值:

RewriteRule mis/(.*) /mis/a-level/$1?blue=9 [R]

有一些帮助http://wiki.apache.org/httpd/RewriteQueryString

答案 2 :(得分:0)

RewriteEngine On

RewriteRule ^a-level$ a-level/
RewriteRule a-level/(.*)$ $1?mode=0 [QSA]

第一条规则作为第二条规则的支持,因为第二条规则需要尾部斜杠。您还可以将[R=301,L]添加到第一个规则,以将用户重定向到带斜杠的版本。 (对于真实的,实际的文件夹,Apache会自动执行此操作。由于这不是一个“真正的”文件夹,您必须自己完成。)

第二条规则将简单地剥离URL的a级部分。然而,使这道菜如此低热量的秘密酱是QSA的一部分。 QSA代表查询字符串追加。它只会将任何现有查询字符串绑定到URL的末尾。

这样做的另一个好处是您不会对QS的内容做出任何假设。如果你需要添加另一个查询变量,除了mainid之外,你不需要修改规则。

编辑:或者,像这样,如果htaccess文件在web根目录中。

RewriteRule ^mis/a-level$ mis/a-level/
RewriteRule mis/a-level/(.*)$ /mis/$1?mode=0 [QSA]

答案 3 :(得分:0)

RewriteEngine On
RewriteCond %{query_string} mode=0
RewriteRule mis/(.+?\.php)$ mis/a-level/$1 [QSA,L]

答案 4 :(得分:0)

我认为你正在以错误的方式看待这个问题 - 你的主要问题是将PHP代码所需的内容翻译成描述URL的内容,而不是通过mod_rewrite尝试完成此操作,我建议你重新考虑你的方法。 / p>

在代码中使用mode = 0是没有意义的,并且对于阅读代码的人来说,它的作用并不明显。我建议改为将代码更改为接受mode = a-level。这将使您的mod_rewrite规则变得更加容易,并且使您的代码更容易阅读。

答案 5 :(得分:-1)

尚未在实时服务器上进行测试,但http://www.regextester.com/显示了您需要的输出

Options +FollowSymLinks
RewriteEngine on

RewriteRule a-level/(.*)\.php\?mainid=(.*)$ /mis/$1.php?mainid=$2&mode=0
RewriteRule a-level/(.*)\.php$ /mis/$1.php?mode=0
RewriteRule a-level$ /mis/index.php?mode=0