简体   繁体   English

重新写入URL规则

[英]ReWrite Rule for URLS

I have 2000 URLs in the format: - 我有2000个网址,格式为:-

http://www.example.com/home/2015/02/yet-another-page-title-goes-here.html
http://www.example.com/home/2015/01/another-page-title-goes-here.html
http://www.example.com/home/2014/12/page-title-goes-here.html

These are OLD URLs from WordPress, when the old site had permalinks enabled doing this for the blog posts. 这些是旧站点的启用了永久链接的WordPress的OLD URL。

I want to strip away two parts of these URLS. 我要剥离这些URL的两个部分。 I want to remove /home/ and I want to remove .html at the end. 我想删除/ home /,最后想删除.html。

This would mean that if someone follows an old link (in Google or elsewhere), they will come to the new URL for that content. 这意味着如果有人点击了旧链接(在Google或其他地方),他们将进入该内容的新URL。

Eg somebody tries to access the old expired link: 例如,有人尝试访问旧的过期链接:

http://www.example.com/home/2015/01/another-page-title-goes-here.html

Will be redirected to the new link: - 将重定向到新链接:-

http://www.example.com/2015/01/another-page-title-goes-here

I've tried editing the htaccess file in WordPress for the last couple days with no joy so far. 在过去的几天里,我尝试在WordPress中编辑htaccess文件,但到目前为止没有任何乐趣。 Redirect rules are new for me. 重定向规则对我来说是新的。

I've treid something like this, but it doesn;t work 我已经整理过类似的东西,但是没有用

RewriteRule ^home/(.+)\.html $

And where do I add this code, if it;s correct, in the htaccess file for WordPress with permalinks enabled. 如果启用了永久链接,该代码是否正确添加到WordPress的htaccess文件中的何处。

This is the content of my htaccess file by default. 这是默认情况下我的htaccess文件的内容。 w3cache has added something at the top too. w3cache也在顶部添加了一些内容。

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component  application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
# END W3TC Browser Cache
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Any thoughts would be significantly appreciated, 任何想法将不胜感激,

UPDATED HTACCESS FILE WITH CODE SUGGESTED: 建议使用代码更新的HTACCESS文件:

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
# END W3TC Browser Cache
RewriteEngine On
RewriteRule ^home/(.+)\.html $
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^home/(.+)\.html$ /$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>
# END WordPress

RewriteRule s have multiple parameters. RewriteRule具有多个参数。 In yours, you only specified a URI to match. 您只指定了一个要匹配的URI。 You should be using the following format: 您应该使用以下格式:

RewriteRule ^home/(.+)\.html$ $1 [R,L]

First, we match home/<everything>.html and redirect to <everything , denoted by $1 . 首先,我们匹配home/<everything>.html并重定向到<everything ,用$1表示。

If the new rule works for you, you can safely change R to R=301 to make the change permanent - that is to say that search engines and browsers will cache the redirect for a longer period of time. 如果新规则对您有效,则可以安全地将R更改为R=301以使更改永久生效-也就是说,搜索引擎和浏览器将在更长的时间内缓存重定向。 Without it, you're basically saying that the redirect is temporary, and that it should not be cached. 没有它,您基本上是在说重定向是临时的,并且不应缓存它。

Your .htaccess file should look like this now: 您的.htaccess文件现在应该如下所示:

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
    AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
    <IfModule mod_mime.c>
        # DEFLATE by extension
        AddOutputFilter DEFLATE js css htm html xml
    </IfModule>
</IfModule>
# END W3TC Browser Cache

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^home/(.+)\.html$ $1 [R,L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>
# END Wordpress

Stealing the answer from @Mike, that I broke and fixed again: 窃取@Mike的答案后,我又打断并修复了:

For the permanent change please put this line: 对于永久更改,请输入以下行:

RewriteRule ^home/(.+)\.html$ /$1 [R=301,L]

before the line: 前行:

RewriteRule ^index\.php$ - [L] # this line means never redirect index.php

It should look like this: 它看起来应该像这样:

RewriteEngine On
RewriteBase /
RewriteRule ^home/(.+)\.html$ /$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM