简体   繁体   中英

ReWrite Rule for URLS

I have 2000 URLs in the format: -

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.

I want to strip away two parts of these URLS. I want to remove /home/ and I want to remove .html at the end.

This would mean that if someone follows an old link (in Google or elsewhere), they will come to the new URL for that content.

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

This is the content of my htaccess file by default. w3cache has added something at the top too.

# 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:

# 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. In yours, you only specified a URI to match. 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 .

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. 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:

# 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:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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