【问题标题】:modrewrite RewriteRule problemmod_rewrite 重写规则问题
【发布时间】:2011-02-23 22:16:20
【问题描述】:

我正在使用 Apache 2.2 和 mod_rewrite。我想采用以下网址并将它们映射到另一个网址。模式很简单,如下所示:

http://domain/test/ex1.html -> http://domain/app/index.php/content/?name=ex1

我在 .htaccess 文件中尝试了以下内容(放在 docroot 的 /test/ 目录中):

RewriteEngine On
RewriteBase /app/
RewriteRule (.*)\.html index.php/content/?name=$1

RewriteEngine On
RewriteRule (.*)\.html /app/index.php/content/?name=$1

我不确定反向引用是否正确,因此设置为 $0 和 $2 但似乎从来没有帮助。

我也尝试将 RewriteLogLevel 设置为 9。

有一个步骤几乎就在那里: 重写 'ex1.html' -> 'index.php/content/?name=ex1'

重写日志的最后一行如下: [perdir /var/www/domain/htdocs/test/] 内部重定向与 /app/index.php/content/ [内部重定向]

如何将其重写为 /app/index.php/content/?name=ex1 ?

编辑 因此,在 docroot 文件中将其用作 .htacces 适用于 RedirectMatch:

RewriteEngine On 
RewriteBase /
RedirectMatch ^(.*)/(.*)\.html$ http://domain/app/index.php/content/?name=$2
 #this doesnt work - adding to docroot allows for it to pick up but still using test/ex1 rather than just ex1
#RewriteRule ^(.*)\.html$ /app/index.php/content/?name=$1

任何帮助让底层 RewriteRule(目前已被注释掉)工作将不胜感激。

谢谢

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    在您的示例中,您提到了 domain/test/ 所以我将基于从 /test/*.html 重写为 /app/index.php/content/?name=

    RewriteEngine On
    RewriteBase /
    RewriteRule ^test/(.*)\.html$ /app/index.php/content/?name=$1
    

    应该可以的。

    【讨论】:

    • hmmm.... 重读我原来的帖子,因为这似乎不起作用。我假设这将是位于 docroot 中的 .htaccess 文件?由于某种原因,它仍然无法正常工作。呃..
    • 对于反向引用,它通过了目录的测试,因此 test/ex1 而不仅仅是 ex1 以 $1 的价格插入。感谢您的帮助
    【解决方案2】:

    嗯,我的头顶:

    RewriteCond %{REQUEST_URI} ^/app/(.*)\.html$
    RewriteRule ^/app/(.*)\.html$ /app/index.php/content/?name=$1
    

    这假设重写基数是/

    【讨论】:

    • 看起来像 app -> test based on dir structure above?
    • 我不确定我是否理解您的评论。如果原始 URI 是 /test/foo.html,目的地是 /app/index.php/content/?name=foo,那么请查看我编辑的答案...
    • 我很确定重写中的 /content/ 导致 mod_rewrite 出现问题,因为它在这种情况下考虑了路径(测试)
    • 我假设这在 .htaccess 文件中不起作用。需要一种方法来去除前导目录名称 test
    猜你喜欢
    • 2011-11-25
    • 2012-07-22
    • 2010-12-25
    • 2012-01-23
    • 2013-09-08
    • 1970-01-01
    相关资源
    最近更新 更多