Pretty URL for web application

I know that there are hundreds of articles, tutorials, and Mod Rewrite tutorials, but I usually have a better understanding of examples, so all I ask for is a quick URL translation and I will be on my way (so I I can determine exactly what he is doing, so I can apply it to others)

I have this URL: http://www.example.com/single.php?id=1

Now it would be great if he could follow the same convention as Stack Overflow, where id = 1 is the actual page title.

Do I have a header in a PHP variable if this is any help?

I would appreciate it if someone could include this: http://www.example.com/single/here-is-my-cool-title (where the last bit is dynamic, depending on the PHP variable)

Is it possible?

+1
source share
4 answers

SO actually has a message id and a title in the url, but they will only use the message id. The name is not very suitable, as there may be duplicate messages (same name, different identifiers), name changes, etc. For the url of this question:

/questions/5142095/pretty-urls-for-web-application 

To solve the following:

 /single.php?id=5142095 

The rewrite rule will be:

 RewriteRule ^questions/([0-9]*)/(.*)$ /single.php?id=$1 

What this means is the beginning of uri (after the domain) ^ , the word questions , any length of the numbers `[0

It is looking for:

  • ^ start URI
  • the word questions
  • slash /
  • variable number length [0-9]* (for example, 1, 123, 1234, etc.)
  • slash /
  • any length of any character .* (i.e. question title)
  • $ end URI

When a character match (for example, [0-9] * or. *) Is in brackets, mod_rewrite puts the result of the match in a numeric dollar variable. In the above rule, [0-9]* matches any number (and any length of numbers) and puts it at $1 . The second match .* Matches any length of any character and puts the match at $2 .

Your application will receive a URI on the right side of the rule, and dollar variables are replaced by matches.

+3
source

You are right about the number of articles on rewriting mods - personally, I don’t like to constantly add additional rules, and then worry if this rule will be caught ...

To do this, I prefer to simply pass the lot through the root index file, and then allow the server side to decide what to do with it.

Look at this.

+1
source

Unlike others, this answer will give you a solution not for a specific question, but for the whole problem. Just because it is based on real life experience, not on bare theory.

This answer contains everything you need, including SEO-optimized redirects from old URLs and code to modify old scripts to make them work as usual.

The only thing left for you to do is change the link generation code on your site. Hope not a big deal.

+1
source

You should use mod-rewrite from Apache, here is an example:

  <Directory /www/yoursite/> RewriteEngine On RewriteRule ^single/?(.*)?/?$ article.php?id=$1 [L] </Directory> 

Edit: It is useful to add an integer identifier to your rewrite, for example:

  RewriteRule ^single/([0-9]+)/?(.*)?/?$ article.php?id=$1&text=$2 [L] 
0
source

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


All Articles