如何重写url访问另一个文件夹下的文件

时间:2015-10-01 06:09:08

标签: apache .htaccess mod-rewrite url-rewriting rewrite

如果stylesheets之后的文件然后读取特定文件夹下的文件,如何重写 e.g
如果用户访问

domain1.com/stylesheets/index.css actually read  > domain1.com/app/assets/stylesheets/index.css 

或 如果访问

domain1.com/stylesheets/bundle/index.css read > domain1.com/app/assets/bundle/stylesheets/index.css

我尝试了以下代码,但没有工作......

RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com\.localhost\$ [NC]
RewriteRule ^/stylesheets/(.*)\.css$ app/assets/stylesheets/$1
#or this rule not work too
RewriteRule ^/stylesheets/$ app/assets/stylesheets/$1


<VirtualHost *:80>
  DocumentRoot "/Users/username/Sites/domain1.com"
  ServerName domain1.com
</VirtualHost>

2 个答案:

答案 0 :(得分:0)

首先,您需要根据主机名检查HTTP_HOST(没有任何其他内容,即):

RewriteCond %{HTTP_HOST} ^domain1\.com\$ [NC]

其次,为了能够进一步处理查询,您需要捕获它的一部分(在括号中,即)。

答案 1 :(得分:0)

您可以在vhost配置或站点根目录中使用此规则.htaccess:

RewriteEngine On
RewriteRule ^/?(stylesheets/.+?\.css)$ /app/assets/$1 [L,NC]

如果使用vhost配置,请记住在进行此更改后重新启动Apache。假设app/assets/路径作为/Users/username/Sites/domain1.com/app/assets/

的完整文件系统路径存在