Cudos
Cudos

Reputation: 5894

htaccess redirect goes to wrong URL

I have setup the following rules in .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    // Redirect old blog URLs to new location
    RewriteCond %{HTTP_HOST} ^www.objectsanduse.com/blog/$
    RewriteRule ^(.*)$ http://www.objectsanduse.com/$1 [R=301,L]

    // Wordpress rewrite rules
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

The URL http://www.somedomain.com/blog/2016/01/19/some-post/ redirects correctly to http://www.somedomain.com/2016/01/19/some-post/

But http://www.somedomain.com/blog redirects to http://www.somedomain.com//2015/03/30/some-other-post

Upvotes: 0

Views: 135

Answers (2)

Rounin
Rounin

Reputation: 29511

If I have understood correctly you only need a single rule:

RewriteRule ^(blog\/?)(.*) http://%{HTTP_HOST}/$2 [R=301,L]

This rule takes any old URI starting with blog (optionally followed by a /)

and captures everything that follows (where everything includes "nothing at all").

Then it rewrites the URI, containing the second capture group but without the preceding blog/.

Upvotes: 2

anubhava
anubhava

Reputation: 786091

You have problem in this rule:

// Redirect old blog URLs to new location
RewriteCond %{HTTP_HOST} ^www.objectsanduse.com/blog/$
RewriteRule ^(.*)$ http://www.objectsanduse.com/$1 [R=301,L]

As %{HTTP_HOST} variable only matches domain name without URI. You can fix that rule by playing:

// Redirect old blog URLs to new location
RewriteCond %{HTTP_HOST} ^www\.objectsanduse\.com$
RewriteRule ^blog(/.*)?$ http://%{HTTP_HOST}$1 [NC,R=301,L]

Upvotes: 1

Related Questions