Forum Moderators: phranque

Message Too Old, No Replies

Building SEO friendly urls

Making SEO friendly urls by replacing "_" by "-"

         

webtechi2010

3:12 pm on Oct 11, 2011 (gmt 0)

10+ Year Member



I want to change all "_" to "-" for specific URLs, not sure of levels or counts of them, as below.

http://example.com/section/sample_topic/sub_topic.html or
http://example.com/section/sample_topic/sample_topic/sub_topic.html or http://example.com/section/sample_topic/sample_topic/sample_topic/sub_topic.html and so on ...

Read many articles and got more confused.
Is there any simple working solution to it ?

I am looking for a .htaccess based solution rather than a programmatic one.

Please help. Thanks in advance.

g1smd

9:26 pm on Oct 11, 2011 (gmt 0)

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



Yes, very simple.

Use the same solution that we recently discussed for changing mixed-case URLs all to lower case.

Rewrite (that's rewrite, not redirect) all requests that contain an underscore to a special PHP script.

In the special PHP script, use preg_replace to replace whatever characters need to be replaced in the URL path.

Use the HEADER directive to send both a 301 response and the new location. The location MUST include the protocol and domain.


You could use only htaccess for this but the code is much more complicated and not at all the best way of doing it.

lucy24

9:50 pm on Oct 11, 2011 (gmt 0)

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



I want to change all "_" to "-" for specific URLs, not sure of levels or counts of them, as below.

What does "change" mean?

Have you replaced all your existing _ with - so you're only concerned with people and search engines looking for the old form? Or do you want the displayed url to have - while the "real" form still has _?

webtechi2010

6:28 am on Oct 12, 2011 (gmt 0)

10+ Year Member



Yes, I have replaced all _ with - and concerned with people and search engines looking for the old form having _ in the URL