dongwei3866 2018-10-28 19:21
浏览 80
已采纳

.htaccess文件/隐藏子目录

I tried several solutions found on StackOverflow but none of them seem to work. First, on document root, I have .htaccess file and index.php that detects language and redirect on /fr/ directory or /en/ directory. Then I have index.html file and a directory called "pages" in which they are all my pages. In that way, all urls are like http://example.com/fr/index.html or http://example.com/fr/pages/a.html.

I would like to :

  1. Hide "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. I used this and it worked fine for php but not html...don't know why.

    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

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条回答 默认 最新

  • douzhuan4406 2018-10-28 22:11
    关注
    1. Hide "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. But don't you want to remove index.html and index.php regardless of where they appear in the URL-path? 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.

    1. Hide html and php extension. I used this and it worked fine for php but not html...don't know why.

      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"? 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?

    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). 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

    To clarify, you must first link to 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:

    RewriteRule ^(fr|en)/([^/.]+)$ /$1/pages/$2.html [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答