htaccess路径重写以从不同的文件夹中获取内容

时间:2013-09-20 16:16:26

标签: .htaccess mod-rewrite

你如何在.htaccess中进行简单的路径重写?我需要我的localhost / blog url来访问localhost / assets / blog中的内容。如何在htaccess中实现此规则?

我的完整.htaccess代码:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
</IfModule>

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L,S=4]

# If requested resource exists as a file or directory, skip next three rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=3]

# Blog articles.
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^/?blog/([^/]+)/(\d+)/(\d+)$ blog.php?title=$1&id=$2&reply_to=$3 [L,S=1]

# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^/?blog/([^/]+)/(\d+)$ blog.php?title=$1&id=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog/.*)$ /assets/$1 [L,NC]

# Remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# for copying
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?page=$1 [L]

RewriteRule (.*)\.xml(.*) $1.php$2 [nocase] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app.php?tag=$1 [QSA,L]

# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------

# You can add custom pages to handle 500 or 403 pretty easily, if you like.
ErrorDocument 403 /error.php?type=403
ErrorDocument 404 /error.php?type=404
ErrorDocument 500 /error.php?type=500

1 个答案:

答案 0 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^(blog/.*)$ /assets/$1 [L,NC]

RewriteRule ^assets/ - [L,NC]