Mobile app version of vmapp.org
Login or Join
Martha676

: How to deal with URLs containing underscores I know that hyphnes are preferred to underscores, and I could do a 301 redirect. But some say it's not worth the chaos to change them and I pretty

@Martha676

Posted in: #301Redirect #Url

I know that hyphnes are preferred to underscores, and I could do a 301 redirect. But some say it's not worth the chaos to change them and I pretty much agree. moz.com/community/q/hyphens-vs-underscores
So, my urls look like this:

example.com/post/this-is-post-title_tokenid/


For new posts I will have:

example.com/post/this-is-post-title-tokenid/


I'm assuming I'd have to return a 404 in case of:


new post with an undescore
old post with a hyphen


Is this the right approach?

Or maybe I should do nothing and leave it as it is?

What would you recommend?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Martha676

2 Comments

Sorted by latest first Latest Oldest Best

 

@Si4351233

Back in the day it was best practice to use only dashes in URL's. Underscores were often used in image/file names (because, OS). But now bots are not so nitpicky.

I would do nothing and leave it as it is. Your token is not a relevant index focal, so the char before it is irrelevant anyways. It's not worth changing the archive stuff simply because of an underscore. For the new stuff, it's purely a matter of how you want the URi to look. Modern SEO indexers don't really care if one char is an underscore/dash, they just need to see the words. Personally, I would keep the underscore, since it acts more like a divider between your post and the token.

10% popularity Vote Up Vote Down


 

@Turnbaugh106

It's not a major problem if you have some underscores, Google and Bing have come along way since they first launched. If you want to be politicly correct then you can rewrite underscores to hyphen by editing the .htaccess with something like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule !.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ ------ [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ ----- [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ ---- [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ --- [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ -- [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ - [E=underscores:Yes]

RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) www.example.com/ [R=301,L]


If you would rather use PHP then you can rewrite the URLS using a replace e.g using: $input_uri = $_GET['rewrite_uri']; and $output_uri = str_replace("_", "-", $input_uri); etc.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme