如何更改子目录中文件的链接以便从根管理器访问?

问题描述:

I'm currently coding an Admin Panel, and for the sake of organization, I've made it so that all of the pages are located in a sub-directory instead of the root directory and I want to access these pages in the sub-directory from the root link. For example, the index.php is in the root directory and is accessible from:

http://localhost/index.php

But the rest of the pages are located in a sub-directory like so:

//What the current link is
http://localhost/pages/page.php

//What I want the link to be
http://localhost/page.php

Some of my pages are in sub-directories of the sub-directory like so:

//What the current link is
http://localhost/pages/page1/page.php

//What I want the link to be
http://localhost/page1/page.php

As you can see, I simply want to eliminate the primary sub-directory from the link, which is pages.

I've looked around on the internet and from what I can tell, this is achievable through .htacess but I couldn't find anything that worked.

我目前正在编写一个管理面板,为了组织起见,我已经做到了这一切 页面位于子目录而不是根目录,我想从根链接访问子目录中的这些页面。 例如,index.php位于根目录中,可从以下位置访问: p>

  http://localhost/index.php 
  code>  pre>  
 
 

但其余页面位于子目录中,如下所示: p>

  //当前链接是什么
http:// localhost  /pages/page.php

//我希望链接是什么
http://localhost/page.php 
  code>  pre> 
 
 

我的一些页面 在子目录的子目录中,如下所示: p>

  //当前链接是什么
http://localhost/pages/page1/page.php 
  
 //我希望链接是什么
http://localhost/page1/page.php 
  code>  pre> 
 
 

正如您所看到的,我只想消除 链接中的主要子目录,即 pages code>。 p>

我在互联网上环顾四周,据我所知,这是可以实现的 .htacess code>但我找不到任何有用的东西。 p> div>

You certainly won't find a perfect solution you can simply copy and paste to solve all your tasks. That expectation is a strange on. Instead you are expected to actually understand how the tools you want to use work and to be able to find your own solution. For that there really are enough good examples here on SO and in addition there is an __excellent_ documentation of apache's rewriting module available with just two mouse clicks.

Anyway, her is a suggestion, you still may have to tweak it to your situation:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /pages/$1 [END]

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).