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

Redirect all pages to a sub directory of the website

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

类似于:

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

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

将以下规则放入根目录的 .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/ [R,L]

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

试试这个:

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

http://www.example.com/anything would redirect to http://www.example.com/blog/anything