【问题标题】:mod_rewrite remapping not workingmod_rewrite 重新映射不起作用
【发布时间】:2017-02-06 04:22:58
【问题描述】:

所以我试图让一个 .htaccess 文件工作,它为我的 Web 服务重新映射一些 URL。我的 .htaccess 文件中有以下代码:

 # General Settings
 Options +FollowSymLinks -Indexes
 Allow from all
 AddCharset utf-8 .css .html .php

 RewriteEngine on
 RewriteBase /

 # Option 1: Remove Index.php from URL
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /index.php?/$1 [L]

 # Custom Options: Webservices
 RewriteRule ^applications/([0-9]+)/infobox\.xml$ app/makeInfoBox/$1/ [L]

删除 index.php 工作正常,所以我认为 mod_rewrite 工作正常。但为什么我的 Web 服务规则不起作用?

它应该将https://example.com/applications/258/infobox.xml 重新映射到https://example.com/app/makeInfoBox/258/ 还是不应该?

当我添加 [R] 标志时,它确实有效,但我不想重定向 URL,我希望它重新映射。

感谢您的回答!

编辑: 因此,经过一些测试,我达到了我真的没有想法的地步。这是我的 mod_rewrite 代码:

 # General Settings
 AddCharset utf-8 .css .html .php

 RewriteEngine on
 RewriteBase /

 # Custom Options: Webservices
 RewriteRule ^applications/([0-9]+)/infobox\.xml$ app/makeInfoBox/$1/ [DPI]

 # Option Remove Index.php from URL
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /index.php?/$1 [L]

我现在以高日志级别激活了日志记录。以下是记录的内容:

添加路径信息后缀:/etc/apache2/htdocs/webservices/applications -> /etc/apache2/htdocs/webservices/applications/258/infobox.xml

删除每个目录前缀:/etc/apache2/htdocs/webservices/applications/258/infobox.xml -> applications/258/infobox.xml

将模式 '^applications/([0-9]+)/infobox\.xml$' 应用到 uri 'applications/258/infobox.xml'

重写'applications/258/infobox.xml' -> 'app/makeInfoBox/258/'

添加每个目录前缀:app/makeInfoBox/258/ -> /etc/apache2/htdocs/webservices/app/makeInfoBox/258/

strip per-dir 前缀:/etc/apache2/htdocs/webservices/app/makeInfoBox/258/ -> app/makeInfoBox/258/

将模式 '^(.*)$' 应用到 uri 'app/makeInfoBox/258/'

RewriteCond: input='/etc/apache2/htdocs/webservices/app/makeInfoBox/258/' pattern='!-f' => 匹配

RewriteCond: input='/etc/apache2/htdocs/webservices/app/makeInfoBox/258/' pattern='!-d' => 匹配

重写'app/makeInfoBox/258/' -> '/index.php?/app/makeInfoBox/258/'

拆分uri=/index.php?/app/makeInfoBox/258/ -> uri=/index.php, args=/app/makeInfoBox/258/

尝试将前缀 /etc/apache2/htdocs/webservices/ 替换为 /

试图用上下文前缀替换上下文 docroot /etc/apache2/htdocs/webservices

使用 /index.php 进行内部重定向 [INTERNAL REDIRECT]

去除每个目录前缀:/etc/apache2/htdocs/webservices/index.php -> index.php

将模式 '^applications/([0-9]+)/infobox\.xml$' 应用于 uri 'index.php'

去除每个目录前缀:/etc/apache2/htdocs/webservices/index.php -> index.php

将模式 '^(.*)$' 应用于 uri 'index.php'

RewriteCond: input='/etc/apache2/htdocs/webservices/index.php' pattern='!-f' => 不匹配

通过/etc/apache2/htdocs/webservices/index.php

比较重定向和重新映射时的日志输出表明它几乎完全相同。

我做错了什么?

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    这样试试

    Options -MultiViews
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^applications/([\d]+)/infobox.xml$ app/makeInfoBox/$1/ [L]
    

    【讨论】:

    • 我把它绑起来了,但这不是错误的方式吗?我希望当我访问 example.com/applications/258/infobox.xml 时 mod_rewrite 将其本地映射到 example.com/app/makeInfoBox/258 但不更改 URL。我在我的服务器上安装了 CodeIgniter,并且 URl 是我的一个函数的路径 (controller(app)->methode(makeInfoBox)->Parameter(id=258))
    • 您在浏览器中输入的网址是什么,原始网址是什么,请单独提及?
    • 我输入example.com/applications/258/infobox.xml,我的 apache 应该将其映射到我的 CodeIgniter 函数,该函数以 XML 格式(所以example.com/app/makeInfoBox/258)。所以我尝试访问的xml文件Infobox.xml并不完全存在,它只是函数的输出。
    • 我认为这是我必须弄清楚的一些内部问题。代码应该可以工作,但它仍然保持不变。也许我必须更改一些 Apache 设置?因为重定向有效,但映射似乎无效。有什么建议吗?
    • 好吧,不使用重写它是有效的,但只有在重写后它没有响应,所以我建议你调整规则以使用一些相对相同的 url 进行测试,然后观察结果并应用回你当前的规则.
    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2013-04-26
    • 2014-07-19
    • 2011-09-07
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多