Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 重定向到子域并更改不同路径_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Regex 重定向到子域并更改不同路径

Regex 重定向到子域并更改不同路径,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我已将我的博客更改为子域 i、 e.www.domain.com/blog->blog.domain.com 简单规则修复此路径和所有路径: 1 RewriteCond %{HTTP_HOST} !^blog\.camcloud\.com$ [NC] 2 RewriteRule ^blog$ https://blog.camcloud.com [L,R=301] 3 RewriteRule ^blog/(.*)$ https://blog.camcloud.com/$1 [L,R=301] 这

我已将我的博客更改为子域

i、 e.www.domain.com/blog->blog.domain.com

简单规则修复此路径和所有路径:

1 RewriteCond %{HTTP_HOST} !^blog\.camcloud\.com$ [NC]
2 RewriteRule ^blog$ https://blog.camcloud.com [L,R=301]
3 RewriteRule ^blog/(.*)$ https://blog.camcloud.com/$1 [L,R=301]
这会相应地移动之后的所有对应路径。尽管第一个问题是我是否将第2行和第3行合并为一条规则?我发现仅仅键入www.domain.com/blog不会重定向到blog.domain.com

我的另一个问题是,我以这种方式明确重定向了其他路径:

Redirect 301 /blog/tags/tag/foo https://blog.domain.com/tag/foo
从本质上讲,我删除了“标签”,保留了路径的其余部分(当然,在更改域的同时)。但这不起作用,因为它会留下“标签”:

我猜是因为以前的规则?另一条路径也会发生同样的情况:

Redirect 301 /blog/categories/industry https://blog.domain.com/category/news/industry-news
其中只有少数几个,路径完全不同,所以我认为一个简单的重定向可以工作,但它仍然在url中保留“类别”。所以它被重定向如下:


您正在混合来自两个不同模块的规则
mod_rewrite
RewriteRule
)和
mod_alias
Redirect
)。最好使用
mod_alias
保留所有规则,如下所示:

RewriteEngine On

# specific redirects first
RewriteRule ^blog/categories/industry/?$ https://blog.domain.com/category/news/industry-news [L,NC,R=301]

RewriteRule ^blog/tags/(tag/foo/?)$ https://blog.domain.com/$1 [L,NC,R=301]

# now catch-all rule to redirect everything from /blog to new sub-domain
RewriteRule ^blog(/.*)?$ https://blog.camcloud.com$1 [L,R=301,NC]
  • 这是假设
    /blog/
    目录没有.htaccess
  • 测试此项之前,请确保清除浏览器缓存