【问题标题】:How to redirect a URL using the .htaccess-file?如何使用 .htaccess 文件重定向 URL?
【发布时间】:2018-04-15 10:35:39
【问题描述】:

我的网站有一个库/目录路径,例如:

{root}/pages/{files}.php

我的 index.php 路径如下:{root}/index.php

我希望,当人们访问(例如)“login.php”(存在于 /pages 目录中。该 url 不包含“/pages”时。

所以:

www.website.com/pages/dashboard.php

重定向到

www.website.com/dashboard.php

当人们访问 www.website.com/dashboard.php 时,他们可能会访问此页面。

抱歉解释不好,找不到好词。

编辑:这是我现在的 .htacces

RewriteEngine on

# Rewrite automatic to /index.
RewriteCond %{REQUEST_URI} ^/$

# Second check for if above doesn't do the job [www/https].
RewriteCond %{REQUEST_URI} !^/index [NC]
RewriteRule ^$ /index [R=301,L]

# Delete all the .php and .html extensions from files [url-related].
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

# Prevent people for looking inside the .htacces file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

我确实尝试过这个: RewriteRule ^/?pages/(.*)$ /$1 [R=301,L]

(确实重定向了我,但仍然收到 404 错误。

【问题讨论】:

  • 好吧,仅在 SO 上就有数百万个简单重定向规则的示例。所以问题是:您需要什么作为附加信息?为什么所有这些答案都不能回答您的问题?
  • 我对 .htacces 的了解几乎为零。所以我可以尝试所有这些答案,我显然做到了。但在所有情况下,我最终得到的是: 1. 我无法直接浏览我的文件(所以当我尝试通过 /pages/file.php 时它确实重定向了,但是当访问链接只是 domain/file.php 2 时它没有工作. 或者它工作但重定向到我已经关闭的.php(带扩展名)。对我来说.htacces就像中文。
  • 因此,请添加您所做的尝试以及对问题的准确描述,以便我们提供帮助。
  • 确实为该主题添加了更多信息。

标签: php .htaccess redirect directory root


【解决方案1】:

我怀疑你只能用 .htaccess 做到这一点。 (编辑:你可以,见下面的“编辑”)

你想要的是假装 login.php 在根目录中,但实际上它不是。

编辑:我想我搞混了。您只能使用 .htaccess 文件来执行此操作。但我不推荐它。您必须手动添加所有重定向(或针对不同情况开发表达式)。
相反,我建议使用以下技术。它基本上是自制内容管理系统的开始,用户可以在其中自己创建页面并命名指向该页面的 URL。此外,这一切都可以扩展到支持 ajax 的网站,以实现真正的最新!

仅使用 .htaccess

如果您对控制器技术的命名优势不感兴趣,请替换 .htaccess 文件中的代码,您的登录页面问题就解决了。

RewriteEngine on
#catch www.example.com/login.php and redirect without user's conciousness to www.example.com/pages/login.php
RewriteRule ^(login.php)$ http://www.example.com/pages/login.php [NC,L,QSA]
# Prevent people from looking inside the .htacces file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

如果您想获得更多(并且实际上是有用的)知识,并想学习如何启动具有上述优势的 CMS 的通用技术,请继续阅读!

本例的目标

当用户调用URL“www.example.com/login”时,会加载登录页面(“login.php”)。

设置

您需要根目录中的 .htaccess 文件。
您需要根目录中的“控制器”(我建议使用 index.php)。
您在某处需要内容页面,在本例中,在 root/pages 中有一个名为“login.php”的文件。
Htaccess 会将所有 URL 重定向到这个控制器,它还会传递用户最初调用的 URL。根据传递的 URL,控制器决定加载哪个页面内容。在我们的例子中,它将是 login.php。

控制器

路径:root/index.php

<?php

    function callPage($data) {
        if (isset($data['path'])) {
            // get the correct file for the given path
              switch ($data['path']) {
                  case "login":
                      include("/pages/login.php");
                      break;
                  default:
                      // show 404 error if given path is unknown
                      include("/pages/error.php");
              }
        }
    }

?>

<html>
    <head></head>
    <body>
        <div id="page"><?php callPage($_GET); ?><!-- load called page's content --></div>
    </body>
</html>

示例内容页面

路径:root/pages/login.php

<p>I am a happy login page. See, that I don't need any HTML Tags, a body or a head, because I am meant to be used only when mother index.php calls me up and integrates me. Life's so easy!</p>
<p>And do you know what's the best? I can even run PHP inside myself! Awesome, isn't it?</p>

.htaccess 文件

路径:root/.htaccess

RewriteEngine on
#if called file or directory reallyexists, don't redirect (might lead into a loop)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#redirect every URL (that does not really exist) to index.php
#the URL the user called can be accessed in index.php in $_GET['path']
RewriteRule ^(.*)$ http://www.exampe.com/index.php?path=$1 [NC,L,QSA]

# Prevent people from looking inside the .htacces file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

冲动

这当然是一个非常基本的例子。目前,使用这种技术代替 .htaccess 文件中的简单重定向没有任何优势,只有缺点。但现在要靠你自己来增强它。一些可能性:

  • index.php:将开关替换为数据库调用。它接受用户的 url 并返回相应的“真实” URL(用户永远不会看到的那个)
  • 为页面添加标题(也保存在数据库中)
  • 为您的网站实现 Ajax 支持

    最后,这导致了一个小系统,可以扩展为自己的 CMS

  • 【讨论】:

    • 啊,明白了。你有没有可能帮我解决这个问题?
    • 当然,目前正在努力;)幸运的是,我刚刚在 4 天前完成了这项工作
    • 啊,谢谢!喜欢这里的人们喜欢帮助新人加入社区的方式。
    • 不欢迎,最后一只手洗另一只手;)
    猜你喜欢
    • 2015-11-02
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多