【问题标题】:Hiding sub-folder from url从 url 隐藏子文件夹
【发布时间】:2017-11-01 02:50:53
【问题描述】:

首先,我想告知这是我关于堆栈溢出的第一篇文章,所以如果我做错了什么,我也想告诉我。

我正在尝试构建一个 PHP 脚本,其中包含控制器和模板等文件夹。两者都在其中的一个或另一个上重定向和包含需要 php 文件。一切都可以通过根域目录上的索引访问。所以我的问题是如何隐藏网址,即domain.com/"controller"/activate.phpdomain.com/"templates"/login.php

我让你知道我通过包含在索引中的一些参数 (if) 中调用了“模板”文件夹中的 login.php

我现在将发布我的.htaccess,以便您可以查看那里的过程,而我过去已经尝试隐藏“模板”文件夹。但是随后无法通过在 .htaccess 上进行编码来访问子目录。这就是为什么我必须删除此处发布的代码,因为这些代码不能解决我的情况。

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /

    ErrorDocument 404 /templates/404

    # To externally redirect /dir/abc.php to /dir/abc
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]


</IfModule>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript

到目前为止,我的 .htaccess 具有隐藏 url 上的 .php 扩展名和缓存 Gzip 压缩方法的参数。所有帮助将不胜感激,错误和正确!

【问题讨论】:

    标签: php apache .htaccess url directory


    【解决方案1】:

    所以,如果我没记错的话,你得到了 basepath/ControllerName/Method 之类的东西,并且你想隐藏 ControllerName 部分? 在我看来,使用 MVC 玩 url/friendlyurl 并保持一个相对基本的 htaccess 像

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /smw/
    
    
    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
    

    你可以通过简单的“路由”配置做你想做的事:

    例如:

    获取url和重定向的路由类

            public function __construct(){
        $uri = $_SERVER["REQUEST_URI"];
        $uri = preg_replace("#".BASE_PATH_PATTERN."#i", "", $uri, 1);
        $this->uriExploded = explode("/",  trim($uri, "/")   );
         /*Check if there is another folder */
        $check = preg_match("/something/i", $uri);
        if($check != false)
        {
            /*instruction... */
        }
    

    希望它有所帮助!如果您需要更多信息,请告诉我

    【讨论】:

    • 这不是我的意思。为了帮助您更多,我将为您提供文件夹的详细信息及其工作原理。所以:
    • 根域目录 httpd 托管:**assets** (folder that contains js, css, img, folders within) **controller** (folder that contains php files with classes, functions, and db connections within them) **languages** (folder that contains php files which checks the region and the decide which php file to read vocabulary-arrays) **templates** (folder that contains php files such as login, activation and etc) **.htaccess** (file that contains the specific code you saw above) **index** (php file that include the login php file, which is locating inside templates folder)
    • 假设我们有调用 login.php 和 profil.php 的索引(如果该语句为真)。因此 url 将首先显示 domain.com,然后如果该语句为 true 将重定向到 profile 并且 url 将在 domain.com/templates/profil.php 更改。因此,关于如何“正确”调用和显示页面,我处于两种方法的中间:1)以某种方式使用 .htaccess 来照顾和隐藏子文件夹。 2)通过使用带有函数和类的php文件,在控制器文件夹上显示索引上的所有内容,如果语句正确且url为domain.com/something.php,则可能包括页面
    • 有什么想法或解决方案吗? @younesdiouri
    • 非常感谢。很高兴能提供帮助!实际上,在我看来,我真的建议使用 .htaccess 而不是 include 来处理它。这是处理显示的逻辑方式,正如您所说的更好的加载(包括索引上的所有内容可能会根据您的数据变得有点棘手)。我想知道您是否可以关注 Mvc 处理技术,因为我一直在关注我的 url,并且由于我使用自己的路由参数,所以很长时间以来我从未遇到过这种问题/困境。 @MihailLlaftiu 祝你好运!
    猜你喜欢
    • 2015-04-09
    • 2017-06-06
    • 1970-01-01
    • 2018-04-05
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2014-01-25
    相关资源
    最近更新 更多