Redirecting old site structure to new site structure

How can I do this using htaccess?

My old site used links such as:

domain.com/this+is+a+page

I want to redirect to the new site structure (uses underscores and ends in .html):

domain.com/this_is_a_page.html

However I do not want to redirect domain.com/admin

How many URLs are we talking here?
If it’s only a handful it’s probably easiest to use Redirect, which is part of Apache’s core so always disabled.

For your example you would put the following in a .htaccess file in the root of your website:


Redirect 301 /this+is+a+page http://domain.com/this_is_a_page.html

If there are a lot of URLs you might want to look at mod_rewrite instead. A good starting point for that is Apache’s mod_rewrite

There’s a few thousand. I will take a look at your link

In trying to redirect any url that contains a “+” character to the same page but with .html at the end why doesn’t this work?

RewriteCond %{QUERY_STRING} (\\++)
RewriteRule ^([a-zA-Z]+)$ $1\\.html [R=301,L,NE]

Because you should look at %{REQUEST_URI}, not at %{QUERY_STRING}

If you have an url like

[noparse]www.example.com/some/folder/somewhere?a=1&b=2[/noparse]

Then

[noparse]www.example.com[/noparse] = %{HTTP_HOST}
/some/folder/somehwere = %{REQUEST_URI}
a=1&b=2 = %{QUERY_STRING}

Also, you don’t need to check for \++, but just \+
And since you’re not re-using what you’re capturing the RewriteCond, you don’t need the parentheses there either.

Lastly, since you have spaces in the URLs, you need to include that in the character range of the RewriteRule. So instead of [a-zA-Z] you should use [a-zA-Z\+]

It’s redirecting properly except that it’s adding in the full directory structure

such as http://domain.com/what+there -> http://domain.com/home/user/public_html/what+there.html

Then add a / in front of it:


RewriteCond %{QUERY_STRING} (\\++)
RewriteRule ^([a-zA-Z]+)$ [color="red"]/[/color]$1\\.html [R=301,L,NE]

Empty your browsers cache before you try the new code; browsers cache 301 redirects.