Forum Moderators: phranque

Message Too Old, No Replies

.htaccess - seo friendly & replace underscores

         

bluey81

7:38 pm on Mar 10, 2015 (gmt 0)

10+ Year Member



Hi, i'm struggling to get .htaccess working the way i need it to.

At the moment urls like this

example.com/index.php?TopicID=12_34_56_78&FrameID=12_34_56_78


Are rewritten to this

example.com/12_34_56_78/12_34_56_78/


However soon i'll be rebuilding the urls to use hyphens instead of underscores i.e.

example.com/12-34-56-78/12-34-56-78/


I now need to rewrite & 301 redirect underscores to hyphens to catch anyone still using the old urls.

This is the code i've got so far but its not working. I think i may have things in the wrong order?


RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]


any help would be great. thanks

not2easy

8:01 pm on Mar 10, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi bluey81, welcome to the forums. It helps others to assist if we can separate things a little. The Rewrites you currently have shown here are not all used for the current rewrite and what you have tried that is 'not working'.

What are the URLs before they are rewritten to use underscores? I mean, what are you interested in changing "from" and "to" as your URL structure?

bluey81

8:44 pm on Mar 10, 2015 (gmt 0)

10+ Year Member



Hi, yeah sorry if i was a bit unclear, let me try again.

Basically the website at the moment produces raw URLS like this

example.com/index.php?TopicID=12_34_56_78&FrameID=12_34_56_78


I will be editing the database soon so that all undescores are converted to hyphens. Then all URLs will be like this by default

example.com/index.php?TopicID=12-34-56-78&FrameID=12-34-56-78


I then want to achieve rewritten URL's like this

example.com/12-34-56-78/12-34-56-78/


However, lots of old links around the web to my pages will be like this

example.com/12_34_56_78/12_34_56_78/


So i want to 301 redirect them to the new format using hyphens. Does that make any more sense? You can ignore anything else in my existing .htaccess i'm happy to start over.

phranque

9:18 pm on Mar 10, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, bluey81!


the easiest solution is to internally rewrite all requested URLs containing underscores to a script which provides the proper redirect response.

bluey81

8:28 am on Mar 11, 2015 (gmt 0)

10+ Year Member



Can you give me an example of how i would do that? Do you mean manually create a list of all old URL's and what their redirect response should be? rather than try to dynamically detect underscores and replace them on the fly?

fathom

8:46 am on Mar 11, 2015 (gmt 0)

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



This might not be what you want to hear. But you made major changes to your website that will have no positive effect, in fact, I would expect results to diminish somewhat not improve.

There is zero difference between words_separated_like_this and words-separated-like-this and the starting value of keywords in url is very low.

I highly recommend you undo this development especially since the url are not even keyword based.

lucy24

8:48 am on Mar 11, 2015 (gmt 0)

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



The key question is: How many _ lowlines are in your existing URLs? How many different patterns are there?

If there are just a few types, then it will be straightforward to make RewriteRules involving strings of
([^_]+)_
et cetera as you've started to do, so long as they're in the right order. You don't need or want [^_]* unless your URLs are very weird, because where would you ever have two consecutive lowlines?

But if there's a wide range of different patterns and different possible numbers of underscores, it is simpler to make a single rule like that says

RewriteRule _ /fixup.php [L]


where the unanchored pattern is "any URL that contains even a single _ lowline anywhere". Place this rule before any other redirects -- but after any access-control RewriteRules, if you have them. "fixup.php" will be an extremely short script that globally replaces _ with - (php can do this easily while mod_rewrite requires complicated loops and repetitions) and then issues a 301 redirect.

Incidentally...
Are rewritten to this

I really hope you meant that the URLs are rewritten from this. (From short pretty URL to long ugly URL.)

bluey81

8:19 pm on Mar 11, 2015 (gmt 0)

10+ Year Member



Ahhh, thank you. I've just tried your 'fixup' method. This is my fixup file

<?php
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url = str_replace('_', '-', $url);
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$url);
?>


Does that look right? it seems to be working ok

lucy24

8:34 pm on Mar 11, 2015 (gmt 0)

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



Does that look right?

Don't look at me :) I only speak three words of php. But yes, it really should be that simple. Global replace in php, ending with issuing a redirect.

Important! In general, RewriteRules that end in [L] alone would be placed near the end, after the ones with [R] flag. But in this case, since the purpose of the rewrite is to issue a redirect, make sure your /fixup.php RewriteRule is located near the beginning of your redirects. Function overrides form.

bluey81

7:46 pm on Mar 15, 2015 (gmt 0)

10+ Year Member



This is now working well so thanks everyone. I just wanted to confirm my thoughts on Fathoms quote below

There is zero difference between words_separated_like_this and words-separated-like-this and the starting value of keywords in url is very low.


This is totally untrue. I ran a series of tests when my urls had underscores for pages that really should have ranked highly and they were nowhere to be seen even 10 pages deep in google. Now they have hyphens, the first few i've checked and now result #1 in google.

Underscores and Hyphens ARE treated differently. Underscores are word joiners, hyphens are word separators.