Forum Moderators: phranque
The site uses a CMS, so I was able to "physically" change all the names, I DO NOT need to do any type of MOD_Rewrite... I do need a Redirect rule for the .htaccess file so any inbound requests to the old (underscore) URLs will be redirected (permanently) to the URLs which now contain hyphens.
I believe an .htaccess 301 redirect is the best method... unless someone disagrees and tell me why I should not do this.
Anyway, I need some help with the 301 rule regular expression that means redirect all matching URLs from:
/news/this_is_a_dir/this_is_a_file.htm
/info/this_is_a_dir/this_is_a_file.htm
to:
/news/this-is-a-dir/this-is-a-file.htm
/info/this-is-a-dir/this-is-a-file.htm
NOTE: I only need it to affect files and subdirectories of www.example.com/news/ and www.example.com/info/
TIA
"If a URL is requested for a page in any subdirectory of /news/ or /info/ which contains underscores, perform a 301 redirect to a page which has EXACTLY the same filespec except the underscores are now hyphens".
I used Perl to rename all the sub-dirs and filenames and it was as simple as looping through the filespecs and running $s =~ s/_/-/g; against them...
In the thread [webmasterworld.com...] I found the following, but it seems overly complicated.
Here is what I have which handles the underscores alone:
# Ignore image paths
RewriteCond %{REQUEST_URI}!^(.*)\.(jpg¦gif)$ [NC]
#
RewriteCond %{REQUEST_URI} ^(.*)\_(.*)$
RewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
RewriteCond %{ENV:space_replacer}!^$
RewriteCond %{ENV:space_replacer}!^.*\ .*$
RewriteRule ^.*$ %{ENV:space_replacer} [L]
I don't need to actually replace anything, ("Rewrite"), I just need to redirect, and have the server send a 301 header.
Generally, you are better off using multiple rules and letting them cascade -- Otherwise you would get a redirect for each and every underscore replaced, and that's not good.
Jim
"If a URL is requested for a page in any subdirectory of /news/ or /info/ which contains underscores, perform a 301 redirect to a page which has EXACTLY the same filespec except the underscores are now hyphens".
I've got a redirect going from one directory to a new one with the same filenames but different file extensions (changing .shtml to .html) and it's working fine:
#Redirect to new page if page is there with filename requested
RewriteRule ^directory_name/([^.]+)\.shtml$ http://www.example.com/directory-name/$1.html [R=301,L]
The new pages are linked to directly from "index" pages, and I've also got a directive that returns a 410 if the page is gone (not all were included in the change).
Would there be a better way to do that, that would be simpler?
(I believe RedirectMatch would also work for a change from underscores to hyphens, and do the same thing, wouldn't it?)
Added:
I've still got a few more directories to make similar changes for on that site, there might be a better way to build the mousetrap than how I've done it.
[edited by: Marcia at 10:36 pm (utc) on July 6, 2008]
redirect 301 /info/this_sub_dir/file_name.htm http://www.example.com/info/this-sub-dir/file-name.htm
I'd have to have 500+ of these in .htaccess, (but not have to deal with any quirky Rewrite behavior or limitations on translating the underscores to hyphens).
Now the problem is the server is sending a 200 OK header.
The whole point of this work is to get the SE's to realize the old underscore URLs are 301'd and gone and to drop them from the index and crawl the new hyphenated URLs.
200 OK
Server: Apache
Keep-Alive: timeout=5, max=30
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
arggghhhhh!
Lacking a redirect, there is no way you're going to get a 200-OK on the old URL if those URLs no longer resolve to files.
Flush your browser cache before testing.
Jim