Forum Moderators: phranque

Message Too Old, No Replies

How to rewrite seo friendly links?

         

Diablotik

2:37 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Hello,

I have been helped a lot on this forum in the past so with smile on my face I would like to ask another question as I believe it is a right place to do it.

I am migrating from one froum script to another and would like to rewrite all my links from my old forum to my new forum.
My 'old' forum is running on phpbb2 and my new forum is vBulletin. There is a brilliant tool to rewrite links from phpbb2 -> vB which can be found here: [vbseo.com...]
but the problem is my links are converted to seo friendly look so this tool would need to be modified. Basically the .htaccess would need to be modified to be precise.
For normal phpbb2 the .htaccess looks like that:
RewriteCond %{QUERY_STRING} f=([0-9]+)
RewriteRule viewforum\.php vbseo301.php?action=forum&oldid=%1 [L]

RewriteCond %{QUERY_STRING} t=([0-9]+)
RewriteRule viewtopic\.php vbseo301.php?action=thread&oldid=%1 [L]

RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule viewtopic\.php vbseo301.php?action=post&oldid=%1 [L]

RewriteCond %{QUERY_STRING} u=([0-9]+)
RewriteRule profile\.php vbseo301.php?action=user&oldid=%1 [L]


Ho do I change it to be workable with links like:
http://example.com/name-of-the-thread-seo-friendly-vt46.htm

for topics
http://example.com/username,u,2460.htm

for users
http://example.com/forum-name-vf1.htm

for forum?

Diablotik

8:28 am on Feb 25, 2010 (gmt 0)

10+ Year Member



By looking at the code I understand I should change each RewriteCond line to my new id which is "vt11", "u,1111" and "vf11" where digit is my id on old forum.
How do I do that with my topics. In normal phpbb there is "=" sign but in my links there is no "=" sign?
For users I understand the line with RewriteCond should be:
RewriteCond %{QUERY_STRING} ^u,([0-9]+)

And no "=" for forum ids again - same problem as with topics.

And then there is RewriteRule that need to be change in each line.
How do I change the forum links from "viewtopic" to my seo friendly links?


It is probably obvious for you guys but for me it could be in Chineese signs and I would still understand the same so please do not laugh at me, just help me please.

And also if this does not make sense please ask extra questions as English is not my native language.
Thanks

jdMorgan

4:27 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot solve this problem with mod_rewrite or .htaccess directives. The problem is that .htaccess has no "knowledge" that "name-of-the-thread-seo-friendly-vt46" is associated with "username,u,2460.htm", and has no access to the database to get this association information.

One solution:

Modify your script to:
  • Accept the old 'unfriendly' URL, look up the new URL, and invoke an external redirect the new URL.
  • Accept the new 'friendly' URL, look up the old URL, and if it resolves to an old post, access the file corresponding to that old URL.
  • For request for new posts which always use friendly URLs, simply invoke the main script.

    The first function is tricky, as you must check that the "REDIRECT_STATUS" variable is blank to make sure that this is an original client request before invoking the redirect. Otherwise, the first two functions will countermand each other, resulting in an 'infinite' redirect/rewrite loop.

    If you cannot modify your 'main' script, then consider 'wrapping' it in a new script which does only the three functions above. So the wrapper script will either redirect old URLs to new, change the requested URL-path for old post requests from new to old and invoke the main script, or invoke the main script directly, depending on the requested URL-path and whether it resolves to an old post or a new one.

    Any way you do it, database access is required to solve this kind of problem.

    Jim
  • Diablotik

    4:52 pm on Feb 25, 2010 (gmt 0)

    10+ Year Member



    I do not understand much from what you said to be completly honest but you said:
    The problem is that .htaccess has no "knowledge" that "name-of-the-thread-seo-friendly-vt46" is associated with "username,u,2460.htm", and has no access to the database to get this association information.

    I just want to make it clear that there is no link between "name-of-the-thread-seo-friendly-vt46" and "username,u,2460.htm".
    Maybe I will start again with the question:
    how to redirect
    "example.com/qwerty-vt46" to vbseo301.php?action=thread&oldid=%1
    where 'qwerty' changes depending on link but this is not important for my new URL. I just need to rewrite numbers which are different for each link (in this case 46)

    It is very difficult to explain it but I hope it is clearer now.

    jdMorgan

    5:30 pm on Feb 25, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Well, that would be:

    RewriteRule ^([^-]+-)+vt([0-9]+)$ /vbseo301.php?action=forum&oldid=$2 [L]

    The first sub-pattern matches one or more path-parts not including a hyphen and followed by a hyphen, while still allowing the match to be evaluated mostly in a left-to-right beginning-to-end fashion requiring a minimum number of back-off-and-retry iterations (five in this specific case) before finding a match.

    See the regular-expressions tutorial cited in our Apache Forum Charter for more information.

    Also, please note that "redirect" and "rewrite" are not synonymous. I tend to use the terms "external redirect" (or "client redirect") and "internal rewrite" to make this more clear. A redirect is a URL-to-URL translation, and ends the current HTTP transaction with a response asking the client to re-request the originally-requested resource from a new URL. An internal rewrite is a URL-to-filepath translation which simply "maps" a URL to a non-default internal filepath in the context of the current HTTP transaction, with no further client involvement.

    This isn't life-changing information, but if you ask for a redirect instead of a rewrite, then you may get a redirect. And that could result in negative effects on your search engine rankings.

    Once you get this working, you should add another rule to redirect only direct client requests for the old URLs to the new ones. This is the part that must be done by a script, because to use your current example mod_rewrite has no way to look at "oldid=46" and generate "qwerty".

    In addition, the script should be modified to validate the "qwerty-vt" part of the originally-requested URL, and to either return a 404 or a 301 redirect response if "qwerty-vt" does not correspond with "46". This servers to eliminate the risk of duplicate-content, and to defeat attempts by jealous/malicious competitors to creatively link to URLs such as "example.com/really-awful-products-and-information-from-a-scammer-vt46" and to therefore cause you both duplicate-content problems and reputation-management problems in search results.

    If this is done, the rule above should be modified to pass the entire request to your script as an additional "get parameter" unless your script can use the Request_URI variable to get it directly from the server context.

    Jim

    Diablotik

    6:04 pm on Feb 25, 2010 (gmt 0)

    10+ Year Member



    OK thanks for that post. It is greatly appriciated.

    It still does not work but probably I need to modify this line as well:

    RewriteCond %{QUERY_STRING} f=([0-9]+)

    at the moment I have got it set up like that:

    RewriteCond %{QUERY_STRING} vt([0-9]+)
    RewriteRule ^([^-]+-)+vt([0-9]+)$ /vbseo301.php?action=forum&oldid=$2 [L]


    which is probably wrong. I have changed
    RewriteCond %{QUERY_STRING} f=([0-9]+)
    to RewriteCond %{QUERY_STRING} vt([0-9]+)
    so it matches my link but still no luck.
    Also I do not understand why you changed (...)action=forum&oldid=%1 to (...)action=forum&oldid=$2

    Thanks again, you have been great help and with your advices I hope to get it working soon

    jdMorgan

    8:20 pm on Feb 25, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    The code I posted is a single line. There is no query string on the URL "example.com/qwerty-vt46", so the RewriteCond is not needed.

    Jim

    Diablotik

    8:32 pm on Feb 25, 2010 (gmt 0)

    10+ Year Member



    Ah I understand now but that still does not work for me :(

    I am not sure if I was clear enought in the previous post but all I want from my seo friendly links is the id of my thread on my old forum (which are the numbers after 'vt') rewrite to my script on my new forum after "=" sign. So for example my new link would look like:
    /vbseo301.php?action=forum&oldid=46

    jdMorgan

    4:27 pm on Feb 26, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > to redirect "example.com/qwerty-vt46" to vbseo301.php?action=thread&oldid=%1

    If you want an external redirect (inform the client and update the URL in the browser address bar), then change the syntax by adding a protocol, domain, and 301-redirect flag to:

    RewriteRule ^([^-]+-)+vt([0-9]+)$ http://example.com/vbseo301.php?action=forum&oldid=$2 [R=301,L]

    This rule does exactly what you asked for, and if "it doesn't work" then either there is a problem elsewhere in the code or in server config, or what you asked for isn't what you really want(?)

    Jim

    Diablotik

    5:12 pm on Feb 26, 2010 (gmt 0)

    10+ Year Member



    It is not an external redirect.
    When someone clicks:
    http://example.com/name-of-the-thread-seo-friendly-vt46.htm
    in google all I want is to move that "46" which is thread ID into
    /vbseo301.php?action=thread&oldid=46 [L]

    I have tried this original script with my 'seo friendly links mod' off and it worked fine.
    I just do not know how to extract this thread id (in the example below it is number 46) and paste it into this line as shown above.

    jdMorgan

    5:19 pm on Feb 26, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Sorry, the code I posted does exactly that.

    If it doesn't work, then the problem is elsewhere.

    Also, reporting only that "it doesn't work" tells us almost nothing. Just a few useful things to post in order to facilitate progress would be:

  • How did you test? What URL did you type?
  • What did you expect to happen?
  • What were the actual results?
  • How did these results differ from your expectations?
  • Do you have other working rules in this .htaccess file?
  • Are there any relevant entries in your server error log file?
  • Did you delete your browser cache before testing new code?

    Jim
  • Diablotik

    3:18 am on Feb 27, 2010 (gmt 0)

    10+ Year Member



    It is 3 am and after spending 5 hours trying to figure it out finally there is result!

    The line below is working fine:

    RewriteRule ^[^/]+vt([0-9]+)\.htm vbseo301.php?action=thread&oldid=$1 [L]

    and following this lead I added this line below for topics with more than one page:
    RewriteRule ^[^/]+vt([0-9]+),([0-9]+)\.htm vbseo301.php?action=thread&oldid=$1&start=$2 [L]


    And then I wanted to use the same code for user redirection but it doesn't work (blank page). It is too late and I am too tired now to think so I am posting this line down here with a big hope.
    RewriteRule ^[^/]+u,([0-9]+)\.htm vbseo301.php?action=user&oldid=%1 [L]


    My username path is for example: example.com\username,u,103.htm and all I want is the number to be transferred onto my script. I hope that make sense.

    Regards,
    Michael

    g1smd

    6:44 pm on Feb 27, 2010 (gmt 0)

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



    Is 'username' a number, a word, multiple words, or a mixture of words and numbers, or the literal word 'username'?

    You're not matching the first comma, therefore a user name with a 'u' in it will confuse the system.
    You're matching 'not a slash, followed by u then comma' - that's not the best pattern.

    Diablotik

    6:49 pm on Feb 27, 2010 (gmt 0)

    10+ Year Member



    username is a mixture of words and numbers.
    I am not matching first comma but I am matching second comma after "u". It is impossible for user to have name with comma followed by "u" so that should be OK. Am I right?

    Also I habe noticed that for my redirection from two posts above does not fork links when it points to a post. For example:
    http://example.com/name-of-the-thread-seo-friendly-vt46.htm#12345

    Ho to get rid of anything that is after .htm during redirection process?

    jdMorgan

    4:54 pm on Mar 1, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    It would be very helpful to the on-going discussion here if you would answer the relevant questions I posted above. For one thing, we have no idea what your URLs look like, and therefore cannot begin to suggest regular-expressions pattern to match them... Please remember that "we cannot see your monitor screen."

    Jim

    Diablotik

    10:46 pm on Mar 2, 2010 (gmt 0)

    10+ Year Member



    I posted several times how my urls look like:

    http://example.com/name-of-the-thread-seo-friendly-vt46.htm#12345