繁体   English   中英

如何使用.htaccess文件重定向URL?

[英]How to redirect a URL using the .htaccess-file?

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

{root} / pages / {files} .php

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

我想当人们访问(例如)“ login.php”(位于/ pages目录中时)。该网址不包含“ / pages”。

所以:

www.website.com/pages/dashboard.php

重定向到

wwww.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错误。

我怀疑您只能使用.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)。
您需要在某处放置内容页面,在此示例中,根目录/页面中有一个名为“ 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(用户从未见过的URL)
  • 在页面上添加标题(也保存在数据库中)
  • 为您的网站实施Ajax支持

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

  • 暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM