PHP, htaccess: apply page title in url

I want to apply an HTML page title to a URL

for example here (stackoverflow) the url looks something like this:

http://stackoverflow.com/questions/10000000/get-the-title-of-a-page-url 

you can see the part "get-the-title-of-a-page-url", which is the name of the page

What do I mean when the user goes to spowpost.php? post = 1

the actual url that displays when the page loads will be spowpost.php? post = 1 $ title = .. the_title ..

How can i do this?

EDIT: I was thinking about htaccess, but I don't know this very well, so the tutorial will help in this BASIC case.

+4
source share
3 answers

You can use .htaccess for this. For instance:

 RewriteEngine On RewriteRule ^questions/(\d+)/([az-]+) index.php?id=$1&title=$2 [L] 

Your PHP page (index.php) gets the identifier and title as parameters in $_GET[] :

 echo $_GET['title']; // get-the-title-of-a-page-url 

Then you can use this (or identifier, which is easier) to get the correct item from your data source:

 // Assuming you didn't store the - in the database, replace them with spaces $real_title = str_replace("-", " ", $_GET['title']); $real_title = mysql_real_escape_string($real_title); // Query it with something like SELECT * FROM tbl WHERE LOWER(title) = '$real_title'; 

Assuming you have an id parameter in a URL of some type, it's easier to request it based on this value. Part of the header can only be used to make a readable URL, without having to act on it in PHP.

In reverse, to convert the header to format-like-this-to-use-in-urls , do:

 $url_title = strtolower(str_replace(' ', '-', $original_title)); 

The above assumes that your headers do not contain characters that are illegal in the url ...

 $article_link = "http://example.com/spowpost.php?post=$postid&$title=$url_title"; 

Or to submit to .htaccess:

 $article_link = "http://example.com/spowpost$postid/$url_title"; 
+5
source

From what I understand, your title will be passed to the page as part of the URL. To show this in the title bar, put this in the section:

 <?php $title=urldecode($_GET["title"]); echo "<title>$title</title>"; ?> 

You may need to change parts of this, such as a dash into spaces or something else. If so, use the PHP str_replace function: http://php.net/str_replace

+1
source

Not sure what the problem you are facing, but only in accordance with what you say in your post: anewser will be:

1.Select the identifier from the URL.
2.You are looking in your database for the original name
3. And then display it in the tag in your HTML.

Check if you have a problem with any of the previous points.

0
source

Source: https://habr.com/ru/post/1395302/


All Articles