0

I'm having a problem, or better to say a doubt about how to do.

Let's explain: I have an website similar to a blog where I write articles and they are displayed in the homepage. I want that every time I click on an article it redirects me to the page where the article's content is shown. I save every article in a database with the title, date, content, etc... My doubt is: should I create a .html file for each article or only a file called "article.php" and when I open it, it changes the content getting the data from the database?

Second question: I want that the url of the page changes based of article that I click. For example I click on the article called "Today and Tomorrow", I want that the URL appears as "mysite/today-and-tomorrow/", not "mysite/article.php". I'm a bit confused about this topic so if anyone can help me i'd be very happy. Thanks in advice.

I have tried

var link = 'www.example.com/training/product.html'; link.split('.html')[0]; window.history.replaceState( null, null, link );

But it changes the URL dinamically, so for a few seconds it appears the original URL, in my case it appears "mysite/article.php" and then it changes in "mysite/today-and-tomorrow". I don't think it's properly correct.

I also tried

function openArticle(title){
        var rightTitle = $(title).text();
        rightTitle = rightTitle.toLowerCase();
        rightTitle = rightTitle.substring(rightTitle .indexOf(':')+2); 
        rightTitle = rightTitle.replace(/[^A-Z0-9]+/ig, "-");
        event.preventDefault();
        window.location.href = title.href + "/" + rightTitle;

    }

But when I click on the element the "article.php" remains in the url

Andrea
  • 3
  • 4
  • in href of a tage print dynamic url with php – masoud soroush Mar 03 '17 at 00:15
  • What have you tried? Questions should have a specific problem and show what you have attempted so far. Currently your question is too broad for stackoverflow's format. – Ilion Mar 03 '17 at 00:17

1 Answers1

0

should I create a .html file for each article or only a file called "article.php" and when I open it, it changes the content getting the data from the database?

No you dont have to. You can pass article id as a "query string" to "article.php" and it will take care of providing proper article to the end user.

I want that the url of the page changes based of article that I click. For example I click on the article called "Today and Tomorrow", I want that the URL appears as "mysite/today-and-tomorrow/", not "mysite/article.php".

You are talking about a feature called "URL Rewriting" which produces nice & meaningful URLs. In order to implement it you need to:

  1. Design your database in a way which it can map articles id to its title.(for creating alias)
  2. You have to enable URL Rewrite in your web server. (for example if you are using Apache as web server, you need to enable mod_rewrite).
  3. You have to embed your rules for mapping & routing nice URLs to the original ones in a file called .htaccess. as a sample How to write htaccess rewrite rule for seo friendly url

I was just trying to help you get an overall idea of what you are looking for from technical point of view & as you know Its impossible to cover your required information in just a single post.

Community
  • 1
  • 1
BehradKhodayar
  • 2,313
  • 2
  • 15
  • 31
  • Thanks for your reply. I enabled mod_rewrite in my httpd.conf file in apache, but what have I to do now? – Andrea Mar 03 '17 at 00:44
  • Is your article.php handling articles? who has wrote the code? – BehradKhodayar Mar 03 '17 at 01:01
  • article.php contains only the clicked article content. I have all the articles' title displayed in the index.php. I have wrote all the code – Andrea Mar 03 '17 at 01:08
  • @Andrea Nice. So, now read [this answer here](stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php#answer-812578). It will help you to map your current 'article.php based URLs' to friendly ones. – BehradKhodayar Mar 03 '17 at 01:19
  • @Andrea [some parts of this article may help you](https://daveismyname.blog/blog/creating-a-blog-from-scratch-with-php-part-2-seo-urls) Now, your only remaining task is creating the `.htaccess` file. Try to keep it as simple as possible for now – BehradKhodayar Mar 03 '17 at 01:24
  • I really appreciate your help. I created a .htaccess file in my folder where is also located the "index.php" file and i tried to wrote "RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f Rewriterule ^index$ index.php" inside it, but it doesn't seem to work. What's wrong? – Andrea Mar 03 '17 at 01:45
  • @Andrea You're welcome. Its a bit tricky. google about `.htaccess patterns for url rewrite`. Stackoverflow is a community to help developers learn while solving their problems. Try to accomplish this part ( as the major part) yourself. I'm available for hints. – BehradKhodayar Mar 03 '17 at 01:52
  • @Andrea Say, If you find the answer useful you can accept it for future reference. – BehradKhodayar Mar 03 '17 at 01:56