Apache重写 - 重定向通配符子域和处理内部URL缩短

Pet*_*rus 5 apache mod-rewrite seo redirect

我在重定向通配符子域和处理内部URL缩短时遇到问题.

假设我的应用程序中有一个内部URL缩短器

example.com/b/ABCDE
Run Code Online (Sandbox Code Playgroud)

那会翻译

example.com/book/12345678-the-book-name
Run Code Online (Sandbox Code Playgroud)

引用的脚本/b/(我使用可以处理URL规则的PHP框架)将短ID ABCDE转换为书籍真实ID 12345678(和标题"书名"),然后将书籍的永久URL重定向example.com/book/12345678-the-book-name

所以,每当我在公告板,微博客网站或海报或名片等物理媒体上传播有关图书的链接时,我都会使用短链接(example.com/b/ABCDE而不是永久链接example.com/book/12345678-the-book-name).

接下来,我需要将所有通配符子域重定向到主域(www.example.com),同时保持请求URI,例如

http://random.example.com/book/11111111-some-book -> http://www.example.com/book/11111111-some-book
http://123456.example.com/book/22222222-another-book -> http://www.example.com/book/22222222-another-book
http://abcdefg.example.com/book/33333333-another-book-again -> http://www.example.com/book/33333333-another-book-again
Run Code Online (Sandbox Code Playgroud)

在我使用的所有规则之后添加以下规则

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

因此,具有example.com域的URL并且没有如下所示的前缀

http://example.com/book/11111111-some-book
Run Code Online (Sandbox Code Playgroud)

将转化为

http://www.example.com/book/11111111-some-book
Run Code Online (Sandbox Code Playgroud)

另一个结果是,如果内部URL缩短器使用没有前缀的普通域,则需要两次重定向才能解析.例如,

http://example.com/b/ABCDE
Run Code Online (Sandbox Code Playgroud)

将首先被重定向到

http://www.example.com/b/ABCDE
Run Code Online (Sandbox Code Playgroud)

然后被重定向到

http://www.example.com/book/12345678-the-book-name
Run Code Online (Sandbox Code Playgroud)

实际上,我不介意两次重定向.但我的SEO顾问说,两次重定向对我网站的搜索引擎优化不利.(我还是不知道为什么)

所以我尝试将最后一条规则改为下面

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} ^/b/(.*)$ 
RewriteRule . index.php [L]

RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我不是很擅长配置Apache,但是当我在http://htaccess.madewithlove.be/中模拟上述规则时,它可以工作.但当我将它应用到我的服务器时,它给了我400 Bad Request for the example.com/p/ABCDE.

所以,我的问题是

  1. 我的SEO顾问对他的论点是否正确?是否有任何解释可以支持他或是否存在反驳?
  2. 为什么服务器给出400 Bad Request
  3. 如何修复重定向?我想维护短URL(example.com/b/ABCDE没有www前缀),但仍然在一个重定向.

nik*_*shr 3

你的SEO顾问是对的吗?

与搜索引擎优化一样,答案涉及一些猜测和假设,但他可能是对的。

当您重定向页面时,您会告诉爬虫忘记初始页面,然后再返回来索引目标页面,这会在首次向世界介绍您的页面和页面在搜索中的实际外观之间引入延迟结果。两次重定向意味着延迟加倍。根据搜索引擎的“情绪”,这可能会导致您的 SEO 显着下降(或者在搜索引擎对重定向进行排序时,您的索引 URL 会出现一些混乱)。

为什么您会收到 400 响应

如果您检查RewriteRule的文档,请输入Inside per-server configuration

给定规则 -->结果替换
^/somepath(.*) --> otherpath$1 :无效,不支持
^/somepath(.*) --> /otherpath$1/otherpath/pathinfo

这意味着,在虚拟主机配置中,您必须为您的替换提供绝对路径(最终的路径将返回给 Apache,之前的路径可以是您喜欢的任何路径)。解决400错误的方法:

RewriteRule . /index.php [L]

如何修复重定向

这将取决于你的index.php如何构建它的重定向,但是设置

ServerName www.example.com
UseCanonicalName On
Run Code Online (Sandbox Code Playgroud)

将设置$_SERVER["SERVER_NAME"]www.example.com并且应该导致 URL 指向规范域。

潜在的会议

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.com example.com

    UseCanonicalName On
    RewriteEngine on

    #set the document root
    DocumentRoot /path/to/the/app 

    # if something goes wrong, setup logs to track what happens
    # comment these lines when you're done
    ErrorLog /a/path/to/a/log/file

    RewriteLogLevel 5
    RewriteLog /a/path/to/another/log/file

    # I simplified the conditions, those are equivalent to your rules
    # a RewriteRule tries to match against %{REQUEST_URI}
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^/b/ /index.php [L] 

    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)