Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rule to change underscores to hyphens

Redirect not rewrite.

         

lexipixel

3:37 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've got a site where I just changed all the paths (directory names) and filenames from using underscores to using hyphens.

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

Receptional Andy

4:24 pm on Jul 6, 2008 (gmt 0)



I believe there are some lines you could adapt within the thread Chaining multiple mod rewrite rules [webmasterworld.com]

lexipixel

9:27 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe I have the wrong idea. It seems everything I read about converting "hyphens" and "underscores" in .htaccess requires I know how many hyphens I need to convert and create separate rules for 1, 2, 3.. and up to 8 replacement, (there is a limit?)... Is this true? Can't I just have a single rule that says:

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

jdMorgan

10:10 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite is not a powerful programming language like PERL. Therefore, its support for 'looping' is quite limited -- See the [N] flag on RewriteRule.

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

Marcia

10:18 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"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]

lexipixel

12:17 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rather than trying to get .htaccess to convert the URLs , I tried a literal redirect:

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.


Server: Apache
Keep-Alive: timeout=5, max=30
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

200 OK

arggghhhhh!

jdMorgan

12:47 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use a decent server headers checker, it's likely that you'll find that the server is first sending a 301, then the client comes back requesting the "new" URL, and the 200-OK response that you're seeing above is the result.

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

lexipixel

1:54 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks JD -- I was using the checker in Chris Pederick's (FireFox) Developers Toolbar. I ran it on a web based checker and now see the 301 followed by the 200.

jdMorgan

3:49 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the "Live HTTP Headers" add-on for Firefox/Mozilla.

Jim