Forum Moderators: phranque

Message Too Old, No Replies

Simple 301 Redirect not working. Help!

Getting frustrated...

         

Swanny007

1:21 am on Sep 23, 2008 (gmt 0)

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



OK, here's the deal. I have a folder name with an underscore in it right now and I want to 301 it to "appear" to use a dash instead. Like so:

Actual URL - www.example.com/key_word/
Display URL - www.example.com/key-word/

I just can't get it to work. I've looked over past posts, tried a bunch of different things and I'm just banging my head against the wall now. I just don't get it. I've done rewrites before and they've worked, I think there's something special about the underscore that's causing problems.

First I tried to simply 301 the /index.php file to the root folder...
Here's the rule that *doesn't* work:
RewriteRule ^key_word/index.php$ http://www.example.com/key_word/ [R=301,NC]

Then I figured hey, why not try to match all files in the /key_word/ folder and make it appear as /key-word/ using (*) and [A-Z] stuff. That was even worse.

Help anyone. I'm really just over my head now and would appreciate if someone could help me figure this out. I'm guessing I'm just missing some string or I have the order wrong or something. Thanks.

jdMorgan

3:05 am on Sep 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What file is this rule located in, /example.com/.htaccess, a server config file, or somewhere else?

Do you have any working mod_rewrite code on this server? If not, you'll need to precede the rule with "RewriteEngine on" and possibly also preced that with "Options +FollowSymLinks".

Jim

Swanny007

5:09 am on Sep 23, 2008 (gmt 0)

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



Thanks jdMorgan. It's in the root /.htaccess file.

Yes, I have lots of working lines in there, I have the ReWriteEngine on in there, but not the Options +FollowSymLinks.

I tried some more variations of that code but I'm really stumped. Would it helped if I posted my entire .htaccess?

g1smd

12:30 pm on Sep 23, 2008 (gmt 0)

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



Something like this:

# Redirect underscored request to hyphenated URL.
# RewriteCond %{REQUEST_URI} ^/folder_name
RewriteRule ^folder_name/(.*) http://www.example.com/folder-name/$1 [R=301,L]

# Rewrite hyphenated request to internal filepath with underscore.
RewriteRule ^folder-name/(.*) /folder_name/$1 [L]

It uses one redirect and one rewrite.

In the redirect code you might need to change _ to be \_ instead.

Links on your pages should be updated to remove the underscore and include the hyphen.

For external sites that can't/won't do this, the redirect preserves your external incoming traffic and shows the user the new URL for the content.

jdMorgan

1:44 pm on Sep 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're going to use a redirect and a rewrite that countermand each other, then checking %{THE_REQUEST} in a RewriteCond is going to be required in order to prevent a loop. This is equally important when redirecting index.xyz to "/", since a DirectoryIndex is essentially a rewrite.

Substitute


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder_name

for the commented-out RewriteCond in the previous post.

No need to escape underscores.

Jim

Swanny007

3:12 pm on Sep 23, 2008 (gmt 0)

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



Thanks Jim. I got this line to work:
RewriteRule ^folder_name/(.*) http://www.example.com/folder-name/$1 [R=301,L]
it redirected with a 301.

But the second line (rewrite) didn't work. I decided to just copy the folder_name folder and rename it folder-name.

What still remains is if I type in /folder-name/index.php it 301's to /folder-name/ and keeps looping.

Thanks Jim but I'm just going to leave it mostly alone right now. I don't really need to rename folder_name to folder-name but I still need to figure this one out...

I have the index.php file like so:
/folder_name/index.php
and I have a duplicate page like so:
/folder_name/

Google in WMT is telling me that I have duplicate pages as a result.

I tried these two rules to 301 but neither of them worked:
RewriteRule ^folder_name/index\.php$ http://www.example.com/folder_name/ [R=301,L] and
RewriteRule ^folder_name/index.php$ http://www.example.com/folder_name/ [R=301,L]

That doesn't work. Do you have any suggestions on that one?

g1smd

4:12 pm on Sep 23, 2008 (gmt 0)

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



Those should have worked, but might get into an infinite loop, under some conditions.

The standard code for redirecting index filenames in URLs is published here almost every day.

There's plenty of prior examples, such as half way down the first page here:

[webmasterworld.com...]

Note the further enhancement added further down that page, too.

Swanny007

4:55 pm on Sep 23, 2008 (gmt 0)

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



g1smd, thanks for replying. The thing is I've already tried about a dozen different things, read various forums, searched for examples and for some reason I just can't get this simple redirect to work:
Redirect 301 /folder_name/index.php to /folder_name/

I see these rewrite threads all the time too. I normally wouldn't start a thread but I am really stumped with this underscore problem. Try it for yourself, you'll see the underscore is handled differently.

I really have tried a lot and I just don't get it. I'll keep trying but I was hoping someone had run into this underscore problem before and have a quick solution.

g1smd

5:20 pm on Sep 23, 2008 (gmt 0)

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



I wouldn't use Redirect.

I would use RewriteCond and RewriteRule.

g1smd

5:28 pm on Sep 23, 2008 (gmt 0)

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



I can't see why this wouldn't work:

# Redirect underscored request to hyphenated URL. 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder_name
RewriteRule ^folder_name/(.*) http://www.example.com/folder-name/$1 [R=301,L]

# Rewrite hyphenated request to internal filepath with underscore. 
RewriteRule ^folder-name/(.*) /folder_name/$1 [L]

Clear your browser cache before testing, otherwise you won't see the correct results.