Forum Moderators: phranque

Message Too Old, No Replies

Problem redirecting file names with spaces

error caused when using .htaccess 301 redirect

         

newexample

2:52 pm on Jul 11, 2007 (gmt 0)

10+ Year Member



A website I'm updating has file names with spaces, such as:

  • i am a good cook.htm

    These pages have a high page rank despite the naming convention problem, so I decided to implement a simple 301 redirect using .htaccess. The following command creates an internal server error when I post the .htaccess file (I assume it's the spaces):

  • Redirect 301 /i am a good cook.htm http://www.example.com/i-am-a-good-cook.html

    I also tried, to account for the server-side formatting of the file name when the page with spaces is requested:

  • Redirect 301 /i%20am%20a%20good%20cook.htm http://www.example.com/i-am-a-good-cook.html

    The previous method doesn't create an error, but it also doesn't redirect the file. I'm at a loss for how to resolve this problem.

    [edited by: jdMorgan at 4:16 pm (utc) on July 11, 2007]
    [edit reason] example.com, please. See Terms of Service. [/edit]

  • jdMorgan

    4:40 pm on Jul 11, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Try the following. One or both should work, and please post your results:

    RedirectMatch 301 ^/i\%20am\%20a\%20good\%20cook\.htm$ http://www.example.com/i-am-a-good-cook.html
    RedirectMatch 301 ^/i\ am\ a\ good\ cook\.htm$ http://www.example.com/i-am-a-good-cook.html

    If neither work, then there is a more complex solution using mod_rewrite. But since you appear to prefer mod_alias directives, let's hold off on that unless it's necessary.

    Jim

    [edited by: jdMorgan at 4:40 pm (utc) on July 11, 2007]

    newexample

    9:54 pm on Jul 11, 2007 (gmt 0)

    10+ Year Member



    Thanks for the suggestions Jim.

    The first tactic did not affect the redirect; pages still resolve to the %20-htm pages. The second suggestion caused an internal server error.

    I'm up for trying the mod_rewrite plan.

    g1smd

    11:55 pm on Jul 11, 2007 (gmt 0)

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



    Can you find any way to get rid of spaces and use either hyphens or dots to separate the words?

    Spaces and their %20 equivalents are bad news every time; and underscores aren't much better.

    vincevincevince

    1:26 am on Jul 12, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Just quote the string you want to match with ' ' or " "

    newexample

    2:26 pm on Jul 12, 2007 (gmt 0)

    10+ Year Member



    @g1smd

    My plan is to replace the files with spaces in the name with files that have dashes to separate the words. However, some of the %20-htm file names already have a high page rank assigned by the searched engines.

    Starting over with a fresh naming convention works for the internal website linking; the 301 redirects are so that external links, from followers of the site, won't be broken.

    @vincevincevince

    When using the quotes, would this be an example of the proper format:

     
    RedirectMatch 301 ^/"i am a good cook.htm$" http://www.example.com/i-am-a-good-cook.html

    igorberger

    3:29 pm on Jul 12, 2007 (gmt 0)

    10+ Year Member



    Use this, it works.

    RewriteRule ^hotels/Sylvestorx(.+)The(.+)Cat.html$
    /hotels/Sylvestorx-The-Cat.html [r=301,nc]

    jdMorgan

    3:49 pm on Jul 12, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    newexample,

    I believe vincevincevince is suggesting:


    Redirect 301 "/i am a good cook.htm" http://www.example.com/i-am-a-good-cook.html

    Jim

    newexample

    5:10 pm on Jul 12, 2007 (gmt 0)

    10+ Year Member



    Thanks Jim.

    The solution from vincevincevince did the trick.

    - Tony