Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.3k views
in Q2A Core by

I know its to prevent that two questions with the same title don't get the same url. But on my wordpress sites i also dont use domain.com/123/article, cause if i have the same title it could do like domain.com/same-title-2.

if i create a page it also work perfectly, but in the pages the metatags "keywords" and "descrition" is missing. They also dont have the option to vote it or post comments :/ If you can solve that in pages, it would make me even more happy.

I think it should't be so hard to do that, but i cant really code, so if there is someone who can help me removing the number, bringing it to the end, or making the page-feature better, so that i can have good looking and seo-perfect urls, please tell me how much you want for it =)

3 Answers

0 votes
by
I got as far as making it accept urls the way you want; I couldn't figure out how to get the urls to change.  I'll take another look though... probably the easiest would be to ask Gideon to implement it, it may be that he can do it easily.  The problem is it seems to be pretty hard-coded, not very extensible.
0 votes
by
I think this number is a key in database where the question information is stored.

In this scenario: SITE.com/123/my-first-question

Q2A does not use "my-first-question" to find an answer in DB (like wordpress does it), it uses "123".
To test it - try to access: SITE.com/123/my-question-blah-blah - you'll see it will point to the same question. While:
SITE.com/111/my-first-question - will point to toally different question.

In the best case it could be replaced with more ugly:

SITE.com/my-first-question?n=123 - or something like that. But i think how it already implemented is good enough.

Why it bothers you though?
by
That's amazing script
by
Cause i want to give my users also tips, like "fitness.com/slimfast" on that site i tell them a lot about the keyword "slimfast". Its cleaner to look at and feels more valuable than "fitness.com/385/slimfast" doesnt it?
I would love to use pages, but like i wrote on top, they are not so well developed :/
+3 votes
by
The key is kungfu.  A backflip taking a punch, and a backflip giving one.

Modify qa-index.php:

        if (isset($_GET['qa-rewrite'])) { // URLs rewritten by .htaccess
                $qa_used_url_format=QA_URL_FORMAT_NEAT;
                $requesturl=qa_gpc_to_string($_GET['qa-rewrite']);
                $requestparts=explode('/', $requesturl);
                if(preg_match('/^[-a-z]+\/[0-9]+$/',$requesturl)) {
                        $no = array_pop($requestparts);
                        array_unshift($requestparts,$no);
                }
                unset($_GET['qa-rewrite']);
                $relativedepth=count($requestparts);
                // Workaround for fact that Apache unescapes characters while rewriting, based on assumption that $_GET['qa-rewrite'] has
                // right path depth, which is true do long as there are only escaped characters in the last part of the path
                if (!empty($_SERVER['REQUEST_URI'])) {
                        $origpath=$_SERVER['REQUEST_URI'];
                        if(preg_match('/^\/[-a-z]+\/[0-9]+$/',$origpath)) {
                                $pa=explode('/', substr($origpath,1));
                                $no = array_pop($pa);
                                array_unshift($pa,$no);
                                $origpath = implode('/',$pa);
                        }
                        $_GET=array();

                        $questionpos=strpos($origpath, '?');
                        if (is_numeric($questionpos)) {
                                $params=explode('&', substr($origpath, $questionpos+1));

                                foreach ($params as $param)
                                        if (preg_match('/^([^\=]*)(\=(.*))?$/', $param, $matches))
                                                $_GET[urldecode($matches[1])]=qa_string_to_gpc(urldecode(@$matches[3]));

                                $origpath=substr($origpath, 0, $questionpos);
                        }

 and qa-base.php:

                        case QA_URL_FORMAT_NEAT:
                                if(preg_match('/^[0-9]+\/[-a-z]+$/',$requestpath)) {
                                        $up = explode('/',$requestpath);
                                        $no = $up[0];
                                        $title= $up[1];
                                        $url.= $title.'/'.$no;
                                }
                                else $url.= $requestpath;
                                break;
 

This performs two backflips, one when receiving a url, and one when outputting it.  I don't know if it will really work with answers, etc., but this is the easiest way I can think of, and it does work with simple question urls.  It changes the output format to:

site.com/question-title/number

but only if the request is in the form number/question-title.
by
Wow, this even modifies the sitemap.xml file properly.  Works with answers as well... That's some kungfu.
by
Really great done! love your kungfu, but is it also possible to not use another subdirectory like question-123 instead of question/123/ ? cause now we still got 2 subdirectories, even if i like that the number is not on the front anymore =)
I think the best way would be to optimize the page-feature so that its possible to use for important "articles". It doesnt use ID at all.
by
edited by
Have you even taken a look at the php code?  As you were already told, the number is essential for looking up the article in the database.  If you want to rewrite the program, go ahead, but this is how it is written, and as was pointed out quite nicely, Stack Exchange doesn't seem to have an SEO problem with the id numbers.  

I would like to see more flexibility in the naming, but it's not really that big of a deal, not something to pay money for or lose sleep over.  Google it before you make assumptions.

And just as a note, the above script won't really work, you need to tweak it for GET requests (?state=answer, etc.)
by
It seems i underestimated this :/ I though it works on pages so its not a big deal to make it work on questions, but now i understand it a little bit better. The only possibility would be making the pages more advanced, so that they could be used if there is something very important...
However thank you for your help! =)
by
+1 for the kung-fu !
...