简体   繁体   English

htaccess主页重写

[英]Home page htaccess rewrite

Thanks for taking the time for reading this. 感谢您抽出宝贵的时间阅读本文。

I have a PHP project with a custom structure, in which the home page is inside multiple folders: 我有一个具有自定义结构的PHP项目,其中主页位于多个文件夹中:

\web\views\site\test.php

I want to be able to load this page when users go directly to the index of my website but I need to keep index's URL. 当用户直接进入我的网站的索引时,我希望能够加载此页面,但是我需要保留索引的URL。 So, if you type 所以,如果您输入

http://example.com/ it should display the contents of http://example.com/web/views/site/test.php but the original URL can't change ( http://example.com/ ). http://example.com/它应该显示的内容http://example.com/web/views/site/test.php但原来的URL不能改变( http://example.com/ )。

Also, if users type http://example.com/web/views/site/test.php it should go to http://example.com/ , so in this case URL has to change. 另外,如果用户键入http://example.com/web/views/site/test.php ,则应转到http://example.com/ ,因此在这种情况下,必须更改URL。

I have tried with other posts I've seen so far but in almost every attempt I get a 500 Internal Server Error . 到目前为止,我已经尝试过其他帖子,但是几乎每次尝试都会收到500 Internal Server Error Is it possible to accomplish this using only htaccess? 是否可以仅使用htaccess来完成此操作?

Thanks! 谢谢!

If i understand you , here you don't need to go for either creating or changing any thing @ .htaccess file . 如果我了解您,在这里您无需创建或更改任何.htaccess文件。

All you have to do is to write the one of the follwoing codes @ index.php in main directory , you may face error according to your server configuration,but try them one by one and you will get it : 您要做的就是在主目录中编写以下一种代码@ index.php, 根据您的服务器配置,您可能会遇到错误,但是请一一试一下,您会得到:

 <?php

  $test = file_get_contents("web/views/site/test.php");
  echo ($test); // option 1


 include("web/views/site/test.php");// option 2
 require('web/views/site/test.php'); // option 3
 require_once('web/views/site/test.php'); // option 4

include($_SERVER['DOCUMENT_ROOT'].'/web/views/site/test.php');// option 5
require($_SERVER['DOCUMENT_ROOT'].'/web/views/site/test.php');// option 6
require_once($_SERVER['DOCUMENT_ROOT'].'/web/views/site/test.php');//option 7     
?>

Note : make sure you put / before the path only with options 5,6 and 7 注意:请确保仅在选项5、6和7的前面加上/

UPDATE regarding your comment below , I think what you need is the following code : 关于下面您的评论的更新,我认为您需要的是以下代码:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yoursite.com$
RewriteCond %{REQUEST_URI} !^/web/views/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /web/views/site/$1
RewriteCond %{HTTP_HOST} ^(www.)?yoursite.com$
RewriteRule ^(/)?$ /web/views/site/index.php [L]

The above code will let you open every thing at main directory from web/views/site and from the path as well , so when you request yoursite.come/test.php is the same like when you request yoursite.come/web/views/site/test.php , so diffrent URLs with same contents 上面的代码将使您可以从web / views / site以及路径中打开主目录中的所有内容,因此,当您请求yoursite.come / test.php时,就像您请求yoursite.come / web / views时一样/site/test.php,因此具有相同内容的不同URL

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

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