Wordpress .htaccess 重定向文件扩展名且不区分大小写

发布于 2024-11-15 19:04:04 字数 2761 浏览 2 评论 0 原文

我正在将网站迁移到 WordPress,但出于 SEO 目的,我想保留当前的 ​​URL 结构。

当前站点使用 .htm 和 .html 文件的组合。我发现当请求是 .html 时,可以成功重定向到 .htm。

例如,

    RedirectMatch 301 (.*)\wp-site/mywebpage.html$ http://www.mydomain.com/wp-site/mywebpage.htm

我遇到以下问题:

当 .html 文件有大写和小写时。我似乎找不到重定向任何请求的解决方案,尽管情况是其 .htm 等效项

例如:My-Posts/Alpha-Page.html 需要重定向到 my-posts/alpha-page.htm 或 MY-POSTS /ALPHA-PAge.html 重定向到 my-posts/alpha-page.htm (或任何包含大写字母的“my-posts/alpha-page”。基本上所有请求,尽管大小写都需要重定向到所有小写字母.htm 文件)

我知道这有点困难,因为它是 1)我重定向到 .htm 和 2)不区分大小写的组合。

需要考虑的几件事:

  1. 我知道 WordPress 帖子是 不区分大小写。 WordPress 的静态 页面区分大小写。
  2. 我知道 RedirectMatch 不起作用 因为它完全匹配。
  3. 我相信我需要使用 RewriteCond 和 RewriteRule,但似乎不能 一步步寻找“好” 解释。还有,我不 了解它们的变量(即: %{HTTP_HOSTS}) 和常规的 表达约定。
  4. 我不是 Apache/.htaccess 或 任何正则表达式专家 方法。

从理论上讲,似乎应该有一种方法可以发挥作用。我将继续查看是否可以在 stackoverflow 或其他外部资源中找到解决方案,如果我确实在 stackoverflow 中找到解决方案,我会将其标记为已解决并将其删除。

任何帮助表示赞赏!

谢谢

更新06/17/2011:

这是我当前的.htaccess文件

    # BEGIN WordPress

    <IfModule mod_rewrite.c>


    RewriteEngine On


    RewriteBase /wp-test-2011-06-13/

    RewriteRule ^index\.php$ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule . /wp-test-2011-06-13/index.php


    RedirectMatch 301 (.*)\wp-test-2011-06-13/redirect.html$ http://www.mydomain.com/wp-test-2011-06-13/redirect.htm


    RewriteRule ^ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME.html$ /orlando-criminal-defense-attorney/defense-attorney-nejame.htm [QSA,NC,R=301]


    </IfModule>


    # END WordPress

更新06/20/2011:

这是我当前的.htaccess文件:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine On

    RewriteBase /wp-test-2011-06-13/
    RewriteRule ^/wp-test-2011-06-13/index\.php$ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /wp-test-2011-06-13/index.php 

    RedirectMatch 301 (.*)\wp-test-2011-06-13/redirect.html$ http://www.mydomain.com/wp-test-2011-06-13/redirect.htm

    RewriteRule ^ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME\.html$ /orlando-criminal-defense-attorney/defense-attorney-nejame.htm [QSA,NC,R=301]

    </IfModule>

    # END WordPress

当我输入http://www .mydomain.com/wp-test-2011-06-13/ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME.html (或与 .html 等效的小写字母)

I am moving a website to wordpress but I want to keep the current URL structure for SEO purposes.

The current site uses a combination of .htm and .html files. I found success in redirecting to .htm when the request is .html.

For example

    RedirectMatch 301 (.*)\wp-site/mywebpage.html$ http://www.mydomain.com/wp-site/mywebpage.htm

I have trouble with the following:

when the .html file has upper and lower case. I can't seem to find a solution to redirect any request despite the case to its .htm equivalent

For example: My-Posts/Alpha-Page.html needs to redirect to my-posts/alpha-page.htm or MY-POSTS/ALPHA-PAge.html redirects to my-posts/alpha-page.htm (or any of "my-posts/alpha-page" that contains uppercase. basically all requests despite case needs to redirect to all lowercase .htm file)

I know it's a little difficult because it's a combination 1) I'm redirecting to .htm and 2) for the case insensitivity.

Few things to consider:

  1. I know Wordpress posts are
    case-insensitive. Wordpress's static
    page are case-sensitive.
  2. I know RedirectMatch won't work
    because it matches it exactly.
  3. I believe I need to use RewriteCond
    and RewriteRule, but can't seem to
    find "good" step by step
    explanations. Also, I don't
    understand their variables (ie:
    %{HTTP_HOSTS}) and the regular
    expression convention.
  4. I am not an Apache/.htaccess or
    regular expressions expert by any
    means.

Theoretically, it seems there should be a way for it to work. I will continue to see if I can find solutions here at stackoverflow or other outside resources and if I do find a solution here at stackoverflow, I will marked this as solved and have it removed.

Any help is appreciated!

Thanks

UPDATE 06/17/2011:

Here is my current .htaccess file

    # BEGIN WordPress

    <IfModule mod_rewrite.c>


    RewriteEngine On


    RewriteBase /wp-test-2011-06-13/

    RewriteRule ^index\.php$ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule . /wp-test-2011-06-13/index.php


    RedirectMatch 301 (.*)\wp-test-2011-06-13/redirect.html$ http://www.mydomain.com/wp-test-2011-06-13/redirect.htm


    RewriteRule ^ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME.html$ /orlando-criminal-defense-attorney/defense-attorney-nejame.htm [QSA,NC,R=301]


    </IfModule>


    # END WordPress

UPDATE 06/20/2011:

Here is my current .htaccess file:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine On

    RewriteBase /wp-test-2011-06-13/
    RewriteRule ^/wp-test-2011-06-13/index\.php$ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /wp-test-2011-06-13/index.php 

    RedirectMatch 301 (.*)\wp-test-2011-06-13/redirect.html$ http://www.mydomain.com/wp-test-2011-06-13/redirect.htm

    RewriteRule ^ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME\.html$ /orlando-criminal-defense-attorney/defense-attorney-nejame.htm [QSA,NC,R=301]

    </IfModule>

    # END WordPress

I get a Wordpress "Page Not Found" page when I type http://www.mydomain.com/wp-test-2011-06-13/ORLANDO-criminal-defense-attorney/defense-attorney-NEJAME.html (or lowercase equivalent with .html)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-11-22 19:04:04

1) 是的,您可以在 mod_rewrite 的帮助下“轻松”执行此操作。但是这些说明需要放置在配置文件中(在 < 内)不幸的是,VirtualHost> 标记)作为 RewriteMap 指令不能在 .htaccess 中使用。

RewriteEngine On
RewriteMap upper2lower int:tolower
RewriteRule ^/(.+)\.html$ /${upper2lower:$1}.htm [QSA,NC,R=301]

2)如果您无法修改配置文件,那么您至少可以将html重定向到htm

RewriteRule ^(.+)\.html$ /$1.htm [QSA,NC,R=301]

还有一些选项可以与 .htaccess 一起使用:

3)为每个这样的页面创建重写规则 - 效率很低并且需要不断更新(每个新页面的新规则) - 大小写在这里并不重要:

RewriteRule ^MYPAGES/some-Page.html$ /mypages/some-page.htm [QSA,NC,R=301]

4)两步重定向:1)将 html 重定向到某个特殊的 php 文件,该文件在执行时重定向到正确的小写 URL。可行..但丑陋(加上涉及2个重定向)。

如果您无法执行#1,请坚持执行#2,并确保您在

中使用

http://www.google.com/support/webmasters/bin /answer.py?answer=139394

1) Yes, you can "easily" do this with the help of mod_rewrite .. BUT these instructions need to be placed in a config file (inside <VirtualHost> tag) as RewriteMap directive cannot be used in .htaccess unfortunately.

RewriteEngine On
RewriteMap upper2lower int:tolower
RewriteRule ^/(.+)\.html$ /${upper2lower:$1}.htm [QSA,NC,R=301]

2) If you cannot modify config file, then you can do at least redirect html to htm.

RewriteRule ^(.+)\.html$ /$1.htm [QSA,NC,R=301]

There are few more options that can be used with .htaccess:

3) Create rewrite rule for each of such pages -- quite inefficient and requires constant updating (new rule for every new page) -- case does not matter here:

RewriteRule ^MYPAGES/some-Page.html$ /mypages/some-page.htm [QSA,NC,R=301]

4) Make 2-step redirect: 1) redirect html to some special php file, which when executed redirects to a proper lower-cased URL. Doable .. but ugly (plus 2 redirects involved).

If you cannot do #1, then stick with #2 and make sure you are using <link rel="canonical" href="PROPER_URL_GOES_HERE"/> in your <header>.

http://www.google.com/support/webmasters/bin/answer.py?answer=139394

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文