user0000001
user0000001

Reputation: 2233

.htaccess Rewrite URL - Adjust to correct path

I've moved a blog over to another host and there are several posts with links that are now broken. This is because I've moved the blog into a subdirectory /blog/. It was previously in the root directory.

I'm hoping I can redirect any URL requests that result in a 404 from:

http://example.com/2014/04/10/the-blog-post

TO

http://example.com/blog/2014/04/10/the-blog-post

So essentially all the htaccess file needs to do is add "blog" in before the query string IF the requested URL does not exist.

Thank you!

Upvotes: 1

Views: 56

Answers (1)

zx81
zx81

Reputation: 41838

Try this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(?!blog) blog%{REQUEST_URI} [L,R,DPI]

Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.

Option 2

For the last line, you can also use this:

RewriteRule ^(?!blog)(.*) blog/$1 [L,R]

Upvotes: 1

Related Questions