如何为目录中的所有文件创建别名?

时间:2012-03-28 09:43:38

标签: .htaccess mod-rewrite url-rewriting

这是我的.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文件中添加它(如上所示)?感谢。

1 个答案:

答案 0 :(得分: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>