将所有页面重定向到网站的子目录

时间:2015-05-12 07:25:31

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

是否可以将网站的所有网页重定向到该网站的子目录,并将原始网址保留在重定向网址的末尾?

类似的东西:

www.example.com/hello-word -> www.example.com/blog/hello-world

或者换句话说,我认为通配符重定向到子文件夹。

2 个答案:

答案 0 :(得分:1)

将以下规则放在根目录中的.htaccess文件中:

RewriteEngine On
# Redirect urls that do not contain path
RewriteRule ^$ /blog/ [R,L]
# Redirect urls that do not map to a directory and file, and do not begin with /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!blog/)(.*) /blog/$1 [R,L]

如果有必要,请在彻底测试规则后将R替换为R=301

答案 1 :(得分:1)

试试这个:

 RedirectMatch 301 ^/(.*)/?$ http://example.com/blog/$1 

http://www.example.com/anything会重定向到http://www.example.com/blog/anything