【问题标题】:Redirect all pages to a sub directory of the website将所有页面重定向到网站的子目录
【发布时间】: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


    【解决方案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

    【讨论】:

      【解决方案2】:

      试试这个:

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

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

      【讨论】:

      • 表示模式匹配的任何字符,
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多