Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根文件夹更改(.htaccess)_.htaccess_Mod Rewrite - Fatal编程技术网

根文件夹更改(.htaccess)

根文件夹更改(.htaccess),.htaccess,mod-rewrite,.htaccess,Mod Rewrite,在我的“public_html”目录中,我有以下结构: - root - index.html - blog - index.html - lab - index.html - wp - (WORDPRESS FILES) “lab”和“wp”目录只是工作正常的子域目录(“”和“”) 基本上,我希望主域(“”)指向“root”目录,而不进行任何实际重定向,例如,我希望“”指向“root”目录中的“index.html”文件,我希望“”指向“root/blog”目录中的“

在我的“public_html”目录中,我有以下结构:

- root
  - index.html
  - blog
    - index.html
- lab
  - index.html
- wp
  - (WORDPRESS FILES)
“lab”和“wp”目录只是工作正常的子域目录(“”和“”)

基本上,我希望主域(“”)指向“root”目录,而不进行任何实际重定向,例如,我希望“”指向“root”目录中的“index.html”文件,我希望“”指向“root/blog”目录中的“index.html”文件等等

我在“.htaccess”文件中使用以下代码实现了这一点:

唯一的问题是,像“”和“”这样的东西在实际上不应该被访问时仍然可以工作(404)

如果有人对如何分类有任何想法,或者有一种更强大的方法,我们将不胜感激

更新

经过数小时的研究,我终于找到了我想要的工作方式,我使用了以下方法:

#  Add directives
RewriteEngine on

#  Change root directory to "root" folder
RewriteCond %{THE_REQUEST} ^GET\ /root/
RewriteRule ^root/(.*) /$1 [L,R=301]
RewriteRule !^root/ root%{REQUEST_URI} [L]

您没有定义任何阻止
/root
地址的规则,因此,如果没有任何规则,您希望如何阻止它

试试这个:

#  Add directives
RewriteEngine on

RewriteCond %{REQUEST_URI} .root [NC]
RewriteRule (.*) / [L,R=404]

#  Remove ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

#  Change root directory to "root" folder
RewriteCond %{HTTP_HOST} ^tomblanchard.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.tomblanchard.co.uk$
RewriteCond %{REQUEST_URI} !.root
RewriteRule (.*) /root/$1 [L,R=301,QSA]

这是未经测试的,因此如果它不起作用,请使用它来满足您的需要。

mod_rewrite中的指令顺序很重要,因为每个规则都将前一个规则的输出视为其测试输入。您需要做3件(或可能4件)事情,以便:

  • 拒绝访问任何以
    /root/
    开头的URL(我们必须先这样做,否则一切都将被拒绝!)
  • 一般来说,确保每个URL只有一个有效表单是一个好的做法,因此指定
    .html
    的URL应该导致浏览器重定向到非
    .html
    表单。这需要在其他重写之前进行,否则您无法区分浏览器中的.html与虚拟添加的.html之间的区别
  • /root/
    目录中查找上面未被拒绝的任何URL,而不是在已配置的
    文档根目录中查找
  • 在URL+
    .html
    下查找任何不指向目录的URL(如果该文件存在)。这必须在其他重写之后进行,否则“文件存在”检查将始终失败

  • PS:请注意,我用谨慎的语言描述(内部)重写,而不是(浏览器)重定向:您的规则不是从任何内容中删除
    .html
    ,而是添加它,从而允许在其他人删除页面时访问该页面。由于您经常在一组规则中修改这两个URL,因此在您的头脑中清楚浏览器请求的URL和Apache最终将提供的虚拟URL之间的区别是很重要的。

    可以改用VHosts。在Apache安装中查找http.conf文件的用法。你可以设置vhosts来指向任何域的任何目录。我很感谢你的回复,但这只会在域下的每个页面上给我一个404。正如我所提到的,这没有经过测试,但我会解释为什么它会这样。首先,当您到达重定向
    http://tomblanchard.co.uk/
    http://tomblanchard.co.uk/blog
    到根目录,然后它假设您在
    根目录下,当它到达阻止
    根目录直接访问的规则时,它不显示任何内容。因为它知道你在那里。重写到名为
    404.html
    的文件不是导致404的正确方法-它仍然会返回状态200,就好像URL是有效的一样。相反,请使用适当的重写标志,例如,或。@IMSoP是的,当然,我只是想将其路由到一个自定义页面以方便使用。@MahanGM那么该自定义页面需要是一个服务器端脚本(例如PHP文件),它可以覆盖状态代码,或者更可能的是,您使用该指令来处理所有404,不管你是否用重写规则人工创建了它们。把
    RewriteBase/
    放在上面不是更好吗?我注意到像
    ^root/
    这样的东西在我的服务器上无法识别,所以我为我的一个项目写了类似
    .root
    的东西。我之所以这么说,是因为有一次它让我头疼。@MahanGM根据具体的上下文,匹配的URL字符串包含或不包含第一个前导
    /
    ;我习惯于访问实际的
    VirtualHost
    定义,而不是
    .htaccess
    文件;您可能是对的,
    RewriteBase
    可能是必要的/有用的。对于
    .root
    ,它将在URL开头以外的任何位置匹配字符串“root”(因为您只需要在它前面有一个字符);你可能想要
    ^/?root
    或者只是
    ^root
    。嘿,非常感谢你在@IMSoP中所做的工作,我刚刚测试过,正如你所看到的,很不幸,每个页面都是404,你知道为什么吗?
    #  Add directives
    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} .root [NC]
    RewriteRule (.*) / [L,R=404]
    
    #  Remove ".html" extension from URLs
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html
    
    #  Change root directory to "root" folder
    RewriteCond %{HTTP_HOST} ^tomblanchard.co.uk$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.tomblanchard.co.uk$
    RewriteCond %{REQUEST_URI} !.root
    RewriteRule (.*) /root/$1 [L,R=301,QSA]
    
    #  General directives
    Options +FollowSymLinks
    RewriteEngine on
    
    # Deny URLs beginning /root/, faking them as a 404 Not Found
    RewriteRule ^root/ [R=404]
    
    # Additional rule to strip .html off URLs in the browser
    RewriteRule ^(.*)\.html$ $1 [R=permanent,L]
    
    # Rewrite everything remaining to the /root sub-directory
    # (Host condition was in your post originally, then edited out; this is where it would go)
    RewriteCond %{HTTP_HOST} ^(www\.)?tomblanchard\.co\.uk$
    RewriteRule ^(.*)$ root/$1
    
    # Handle "missing" ".html" extension from URLs
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html