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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择