【问题标题】:How to create aliases for all files in a directory?如何为目录中的所有文件创建别名?
【发布时间】:2012-03-28 09:43:37
【问题描述】:

这是我的 .htaccess 文件的样子:

# Original
# If you modify this file then change the above line to: # Modified
<IfModule mod_rewrite.c>
   RewriteEngine On
   # Certain hosts may require the following line.
   # If vanilla is in a subfolder then you need to specify it after the /. 
   # (ex. You put Vanilla in /forum so change the next line to: RewriteBase /forum)
   # RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
</IfModule>

我刚刚在 .htaccess 中使用重写规则读到,我可以创建 URL 别名,可用于缩短长 URL。我想创建一个通配符重写规则,以便此目录中的所有文件:http://example.com/cache/Sitemaps/ 缩短(不重定向)为http://example.com/*

例如,如果file1.xml、file2.xml、file3.xml...等是驻留在http://example.com/cache/Sitemaps/中的文件,我想要:

http://example.com/cache/Sitemaps/file1.xmlhttp://example.com/file1.xml
http://example.com/cache/Sitemaps/file2.xmlhttp://example.com/file2.xml
http://example.com/cache/Sitemaps/file3.xmlhttp://example.com/file3.xml
...等等! (其中 → 代表“别名或缩写为”)

我该怎么做?我应该将它添加到我的 .htaccess 文件的什么位置(如上所示)?谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    这是您修改后的 .htaccess,将其放在 DOCUMENT_ROOT 中:

    # Modified
    # If you modify this file then change the above line to: # Modified
    <IfModule mod_rewrite.c>
       RewriteEngine On
       # Certain hosts may require the following line.
       # If vanilla is in a subfolder then you need to specify it after the /. 
       # (ex. You put Vanilla in /forum so change the next line to: RewriteBase /forum)
       RewriteBase /
    
       # shorten /cache/Sitemaps/foo.xml to /foo.xml
       RewriteRule ^((?!cache/Sitemaps/).*\.xml)$ cache/Sitemaps/$1 [NC,L]
    
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
    
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2011-04-11
      相关资源
      最近更新 更多