繁体   English   中英

重新写入URL规则

[英]ReWrite Rule for URLS

我有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

这些是旧站点的启用了永久链接的WordPress的OLD URL。

我要剥离这些URL的两个部分。 我想删除/ home /,最后想删除.html。

这意味着如果有人点击了旧链接(在Google或其他地方),他们将进入该内容的新URL。

例如,有人尝试访问旧的过期链接:

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

将重定向到新链接:-

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

在过去的几天里,我尝试在WordPress中编辑htaccess文件,但到目前为止没有任何乐趣。 重定向规则对我来说是新的。

我已经整理过类似的东西,但是没有用

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

如果启用了永久链接,该代码是否正确添加到WordPress的htaccess文件中的何处。

这是默认情况下我的htaccess文件的内容。 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

任何想法将不胜感激,

建议使用代码更新的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具有多个参数。 您只指定了一个要匹配的URI。 您应该使用以下格式:

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

首先,我们匹配home/<everything>.html并重定向到<everything ,用$1表示。

如果新规则对您有效,则可以安全地将R更改为R=301以使更改永久生效-也就是说,搜索引擎和浏览器将在更长的时间内缓存重定向。 没有它,您基本上是在说重定向是临时的,并且不应缓存它。

您的.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

窃取@Mike的答案后,我又打断并修复了:

对于永久更改,请输入以下行:

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

前行:

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

它看起来应该像这样:

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