htaccess重写图像

时间:2017-09-19 16:53:46

标签: .htaccess mod-rewrite

我的.htaccess文件中包含此代码,该文件重定向main_website文件夹中的所有网页,但当前图像存储在根目录中

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ main_website/index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ main_website/index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]

RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)/?([0-9]+)?$ main_website/index.php?id=$1&type=$2&unique=$3&pagenum=$4 [QSA,NC,L]

RewriteRule ^(account/hosting)/([\w-]+)?$ main_website/index.php?id=$1&p=$2 [QSA,NC,L]

# This will process the /section/(.*) requests, ?section=1 will be appended to query string    
RewriteRule ^section\/(.*)?$ main_website/index.php?section=1&id=$1 [L,QSA]

RewriteRule ^/?$ main_website/index.php [L,QSA]

RewriteRule ^([a-zA-Z0-9-/_]+)/?$ main_website/index.php?id=$1 [L,QSA]

如何更改上述内容以将images文件夹移至main_website文件夹?

1 个答案:

答案 0 :(得分:1)

images文件夹移到main_website/下,然后您就可以在网站根目录中使用这些规则.htaccess:

RewriteEngine On

RewriteRule ^/?$ main_website/index.php [L]

# redirect images to main_website/images/
RewriteRule ^images/.+$ /main_website/$0 [L,NC,R=301,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ main_website/index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]

RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ main_website/index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]

RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)/?([0-9]+)?$ main_website/index.php?id=$1&type=$2&unique=$3&pagenum=$4 [QSA,NC,L]

RewriteRule ^(account/hosting)/([\w-]+)?$ main_website/index.php?id=$1&p=$2 [QSA,NC,L]

# This will process the /section/(.*) requests, ?section=1 will be appended to query string    
RewriteRule ^section\/(.*)?$ main_website/index.php?section=1&id=$1 [L,QSA]

RewriteRule ^([\w/-]+)/?$ main_website/index.php?id=$1 [L,QSA]