【问题标题】:Internal Redirect - PHP and .htaccess?内部重定向 - PHP 和 .htaccess?
【发布时间】:2012-01-10 02:52:49
【问题描述】:

晚上好, 我想知道是否可以将请求重定向到我的页面,作为http://test.diogo.me 到内容为-> http://www.diogo.me/pages/test/ 的页面(此重定向必须是内部的......浏览器应该指向http://test.diogo.me)。 我一直在阅读有关 .htaccess 的内容,我觉得这就是我的解决方案。
我已尝试使用以下代码:

 <IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

输出是错误 404 file not found :(
我走在正确的轨道上吗?
对不起我的表达限制。提前谢谢

【问题讨论】:

    标签: php .htaccess redirect subdomain


    【解决方案1】:

    只需将此代码放在您的 .htaccess 中,然后从那里删除您现有的行:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^test\.diogo\.me$ [NC]
    RewriteRule ^$ http://www.diogo.me/pages/test/ [L,P]
    

    【讨论】:

    • 或者你可以将R替换为P来代理,所以浏览器仍然显示test.diogo.me
    • 非常感谢@JonLin,将我的答案编辑为使用P 而不是R
    • 感谢@anubhava 和 Jon Lin 的支持。这段代码不起作用,是不是和我服务器的配置有关?
    • 提供一些细节,比如你看到了什么行为?还提供来自 Apache 的 access.log 和 error.log 的匹配日志。
    • access.log 和 error.log 都是空的,它在我的浏览器上给我一个“找不到服务器”异常:(
    【解决方案2】:

    您缺少/pages 部分

    RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
    RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
    RewriteRule ^(.*)$ /pages/%1/$1 [L]
    

    假设子域与主域具有相同的文档根。

    【讨论】:

    • 我的整个 htaccess:&lt;IfModule mod_rewrite.c&gt; RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.diogomend\.me [NC] RewriteCond %{HTTP_HOST} ([^.]+)\.diogomend\.me [NC] RewriteRule ^(.*)$ /pages/%1/$1 [L] &lt;/IfModule&gt; 仍然无法正常工作。该请求甚至没有显示在我的 access.log 中
    猜你喜欢
    • 2016-02-18
    • 2012-06-04
    • 2014-10-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多