.htaccess重定向到新的网站结构 [英] .htaccess to redirect to new website structure

查看:97
本文介绍了.htaccess重定向到新的网站结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了网站URL结构,并希望重定向用户

I have changed the website URL structure and I want to redirect my users

问题

新结构非常不同,并且更加严格.

The new structure is very different, and more strict.

OLD

1 - https://example.com/serie-tv

2 - https://example.com/serie-tv/1845578-the-walking-dead

3 - https://example.com/serie-tv/1845578-The-Walking-Dead/seasons/1

4 - https://example.com/serie-tv/1845578-the-walking-dead/seasons/1/episodes/1

1 - https://example.com/browse?type=series

2 - https://example.com/titles/1845578

3 - https://example.com/titles/1845578/season/1

4 - https://example.com/titles/1845578/season/1/episode/1

https://example.com/serie-tv/1845578-the-walking-dead/seasons/1

https://example.com/titles/1845578-the-walking-dead/season/1不起作用.

只需要重定向到https://example.com/titles/1845578/season/1

目前,我仅设法重定向了 https://example.com/serie-tv /.....https://example.com/browse?type=series

For now I only managed to redirect everything under https://example.com/serie-tv /..... to https://example.com/browse?type=series

使用以下代码:

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^serie\-tv\/?(.*) "https\:\/\/example\.com\/browse?type=series" [R=301,L]

推荐答案

由于您在旧的URL结构中具有seasonsepisodes(带有s)以及seasonepisode(没有s)在新的URL结构中,因此您不能使用简单地复制URL路径的通用解决方案.

A slight complication comes about because you have seasons and episodes (with an s) in the old URL structure and season and episode (no s) in the new URL structure, so you can't use a general solution that simply copies the URL-path.

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$

如果您只有一个域,则这些条件是多余的.

If you only have a single domain then these conditions are superfluous.

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

就目前而言,我们也可以忽略这些.自动更新SSL证书时,cPanel会根据需要自动注入它们.

For now, we can ignore these as well. cPanel will automatically inject these as required when auto-renewing your SSL certs.

目前,我只设法重定向了...下的所有内容.

For now I only managed to redirect everything under ...

我假设您不希望重定向所有内容",因为您前面的示例URL并未说明这一点?

I assume you don't want to redirect "everything" since your preceding example URLs do not state this?

RewriteRule ^serie\-tv\/?(.*) "https\:\/\/example\.com\/browse?type=series" [R=301,L]

此处没有不必要的反斜杠转义,这会影响可读性.无需在RewriteRule 模式中转义文字连字符(-)和斜杠.并且在 substitution 字符串(这是一个普通"字符串,而不是正则表达式)中,无需转义冒号(:),斜杠(再次)和点.这些文字"字符在使用上下文中没有特殊含义. (这是使用cPanel重定向功能的典型输出-经常也会将它们放在错误的位置.)

There is unnecessary backslash escaping here that affects readability. There is no need to escape literal hyphens (-) and slashes in the RewriteRule pattern. And in the substitution string (which is an "ordinary" string, not a regex), there is no need to escape the colon (:), slash (again) and dot. These "literal" characters carry no special meaning in the context they are used. (This is typical output having used cPanel's redirection feature - which will often put them in the wrong place as well.)

.htaccess文件顶部附近尝试以下类似的操作:

Try something like the following instead, near the top of your .htaccess file:

RewriteEngine On

# Redirect "/serie-tv"
RewriteRule ^serie-tv$ /browse?type=series [R=302,L]

# Redirect "/serie-tv/1845578-the-walking-dead"
RewriteRule ^serie-tv/(\d+)-[^/]+$ /titles/$1 [R=302,L]

# Redirect "/serie-tv/1845578-the-walking-dead/seasons/1"
RewriteRule ^serie-tv/(\d+)-[^/]+/seasons/(\d+)$ /titles/$1/season/$2 [R=302,L]

# Redirect "/serie-tv/1845578-the-walking-dead/seasons/1/episodes/1"
RewriteRule ^serie-tv/(\d+)-[^/]+/seasons/(\d+)/episodes/(\d+)$ /titles/$1/season/$2/episode/$3 [R=302,L]

\d是数字的简写字符类(与[0-9]相同),而\d+匹配1个或多个数字.

\d is a shorthand character class for digits (the same as [0-9]) and \d+ matches 1 or more digits.

$1$2$3是对RewriteRule 模式中捕获的组的反向引用.在RewriteRule 模式中测试您可以执行的操作比使用检查REQUEST_URI服务器变量的先前条件更有效.

$1, $2 and $3 are backreferences to the captured groups in the RewriteRule pattern. It's more efficient to test what you can in the RewriteRule pattern instead of using a preceding condition that checks the REQUEST_URI server variable.

请注意,这些当前是302(临时)重定向.测试它们可以正常工作后,请仅将其更改为301(永久),以避免缓存问题.

Note that these are currently 302 (temporary) redirects. Only change to 301 (permanent) when you have tested that they work OK - in order to avoid caching issues.

在测试之前,您需要清除浏览器缓存.

You will need to clear your browser cache before testing.

此外:从SEO/可用性角度来看,包含标题的旧URL结构可能更好?

Aside: The old URL structure, that includes the title maybe better from an SEO / useability perspective?

这篇关于.htaccess重定向到新的网站结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆