简体   繁体   English

.htaccess文件/隐藏子目录

[英].htaccess file / hide subdirectory

I tried several solutions found on StackOverflow but none of them seem to work. 我尝试了在StackOverflow上找到的几种解决方案,但是似乎都没有用。 First, on document root, I have .htaccess file and index.php that detects language and redirect on /fr/ directory or /en/ directory. 首先,在文档根目录上,我有.htaccess文件index.php ,它们可以检测语言并重定向到/fr/目录或/en/目录。 Then I have index.html file and a directory called "pages" in which they are all my pages. 然后,我有了index.html文件和一个名为“ pages”的目录,其中所有目录都属于我的页面。 In that way, all urls are like http://example.com/fr/index.html or http://example.com/fr/pages/a.html . 这样,所有网址都类似于http://example.com/fr/index.htmlhttp://example.com/fr/pages/a.html

I would like to : 我想要 :

  1. Hide " index.html ". 隐藏“ index.html ”。 If I'm not mistaken, this solution is fine : 如果我没记错的话,这个解决方案很好:

     RewriteEngine On RewriteRule ^index\\.html$ / [R=301,L] 
  2. Hide html and php extension. 隐藏htmlphp扩展名。 I used this and it worked fine for php but not html...don't know why. 我用了它,它对php很好用,但是对html却不行。。。不知道为什么。

     RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\\.]+?)/?$ $1.php [NC,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\\.]+?)/?$ $1.html [NC,L] 
  3. Hide pages directory (i don't mind if /fr/ or /en/ directories are shown) so that URL will be like this (in the end) : http://example.com/fr/a 隐藏页面目录(我不介意显示/ fr /或/ en /目录),以便URL像这样(最后): http : //example.com/fr/a

Here is my structure 这是我的结构

├── fr
|   └── index.html
|   └── pages
|       └──a.html
|       └──b.html etc
├── en
|   └── index.html
|   └── pages
|       └──a.html
|       └──b.html etc
├── .htaccess
└── index.php
  1. Hide "index.html". 隐藏“ index.html”。 If I'm not mistaken, this solution is fine : 如果我没记错的话,这个解决方案很好:

     RewriteEngine On RewriteRule ^index\\.html$ / [R=301,L] 

You say "fine", however, this does not appear to correlate with your site structure, so it wouldn't appear to do anything? 您说“很好”,但是,这似乎与您的网站结构不相关,因此它似乎无能为力吗? This directive assumes you have index.html in the document root. 该指令假设您在文档根目录中有index.html But don't you want to remove index.html and index.php regardless of where they appear in the URL-path? 但是,您是否要删除index.htmlindex.php无论它们出现在URL路径中的什么位置? For example, something like: 例如,类似:

RewriteCond %{THE_REQUEST} /index\.(html|php)
RewriteRule (.*)index\.(html|php)$ /$1 [R=301,L]

The condition is to prevent a redirect loop on certain server configs. 条件是防止某些服务器配置上的重定向循环。

This also requires that both index.php and index.html are included in your DirectoryIndex. 这还要求index.phpindex.html都包含在DirectoryIndex.

  1. Hide html and php extension. 隐藏html和php扩展名。 I used this and it worked fine for php but not html...don't know why. 我用了它,它对php很好用,但是对html却不行。。。不知道为什么。

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

This is a conflict . 这是一个冲突 You have the same RewriteRule pattern - so which should "win"? 您具有相同的RewriteRule 模式 -那么哪个应该“获胜”? The first directive will always "win", the second directive will never happen. 第一条指令将永远“赢”,第二条指令将永远不会发生。

But... is your "site structure" not complete? 但是...您的“网站结构”不完整吗? Your site structure shows a single index.php in the document root (which is redirected to / anyway) - there are no .php files in subdirectories, so why the need to check? 您的站点结构在文档根目录中显示了一个index.php (始终重定向到/ )-子目录中没有.php文件,那么为什么需要检查?

If you do have a mix of .html and .php files throughout your site structure, with no discernible pattern then you will need to perform a filesystem check to see if the file exists before rewriting to it (which is relatively expensive). 如果在整个站点结构中确实混合了.html.php文件,并且没有可识别的模式,则需要在重新写入文件之前执行文件系统检查以查看文件是否存在(这相对昂贵)。 For example: 例如:

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

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

There is no need to backslash-escape a dot (to match a literal dot) when used inside a character class, as it carries no special meaning when used inside the character class (as opposed to elsewhere in the regex). 在字符类中使用时,不需要反斜杠转义点(以匹配文字点),因为在字符类中使用时,它不带有特殊含义(与正则表达式中的其他地方相反)。

However, if there is a pattern , then this can be greatly improved. 但是,如果有模式 ,则可以大大改善。

  1. Hide pages directory (I don't mind if /fr/ or /en/ directories are shown) so that URL will be like this (in the end) : http://example.com/fr/a 隐藏页面目录(我不介意显示/fr//en/目录),以便URL像这样(最后): http://example.com/fr/a : http://example.com/fr/a

To clarify, you must first link to example.com/fr/a . 为了澄清,您必须首先链接到example.com/fr/a

Then to rewrite all requests to the /pages subdirectory (and append the .html extension in the same motion), you can do something like the following: 然后,将所有请求重写到/pages子目录(并以相同的动作附加.html扩展名),您可以执行以下操作:

RewriteRule ^(fr|en)/([^/.]+)$ /$1/pages/$2.html [L]

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

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