dongwei3866
dongwei3866
2018-10-28 19:21

.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 douzhuan4406 3年前
    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]
    
    点赞 评论 复制链接分享

相关推荐