Forum Moderators: phranque
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.
# 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.
Substitute
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder_name
No need to escape underscores.
Jim
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?
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.
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.
# 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.