【问题标题】:Remove subdirectory from the url makes page not load (404 error)从 url 中删除子目录会使页面无法加载(404 错误)
【发布时间】:2014-05-22 07:54:17
【问题描述】:

我想从 url 中删除目录以从 www.mysite.com/pages/test.php 转到 www.mysite.com/test.php

我试过了:

RewriteEngine On
RewriteBase /

# externally redirect /dir/foo to /foo
RewriteCond %{THE_REQUEST} pages/
RewriteRule pages/(.*)$ $1 [L,NC,R]

# internally forward /foo to /dir/foo
# WHAT GOES HERE?

它在 url 中显示 www.mysite.com/test.php 但页面没有加载!如果我删除 htaccess 代码,页面 www.mysite.com/pages/test.php 加载没有问题..

请帮我解决这个问题!非常感谢所有帮助!

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:

    确保您已启用 mod_rewrite

    【讨论】:

    • 我如何在 Godaddy 和 localhost xampp 上做到这一点?我读到 Godaddy 已经启用了 mod_rewrite
    • 知道怎么做吗?
    • 对于 Windows 平台:要激活模块,需要激活 httpd.conf 中的以下行: LoadModule rewrite_module modules/mod_rewrite.so *Restart apache For Linux Platform: a2enmod rewrite *Reload all apache config文件:服务 apache2 重启
    【解决方案2】:

    按照这些步骤 通过__FILE__php魔术常数获取文件路径 拷贝文件 并删除旧文件

    <?php $file = __FILE__; $newfile = '/';
    
    if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n"; }  delete("$fil
    e"); ?>
    

    【讨论】:

    • 我不想复制文件。我希望它保留在我的服务器上的 pages/ 中,但我希望 url 删除 pages/
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2021-04-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多