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 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作