【发布时间】:2023-03-20 21:45:01
【问题描述】:
是否可以将网站的所有页面重定向到该网站的子目录,并将原始 URL 保留在重定向 URL 的末尾?
类似:
www.example.com/hello-word -> www.example.com/blog/hello-world
或者换句话说,我认为是通配符重定向到子文件夹。
【问题讨论】:
标签: apache .htaccess mod-rewrite url-redirection
是否可以将网站的所有页面重定向到该网站的子目录,并将原始 URL 保留在重定向 URL 的末尾?
类似:
www.example.com/hello-word -> www.example.com/blog/hello-world
或者换句话说,我认为是通配符重定向到子文件夹。
【问题讨论】:
标签: apache .htaccess mod-rewrite url-redirection
将以下规则放入根目录下的.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。
【讨论】:
试试这个:
RedirectMatch 301 ^/(.*)/?$ http://example.com/blog/$1
http://www.example.com/anything 将重定向到http://www.example.com/blog/anything
【讨论】: