Asked  8 Months ago    Answers:  5   Viewed   33 times

I am currently working on a blog where I would like to create links to my individual articles in the following form:

http://www.mysite.com/health/2013/08/25/some-random-title
                      ------            -----------------
                        |                       |
                     category                 title

However I have no idea how to achieve this.

I have found something that would give me the URI.

$uri = $_SERVER["REQUEST_URI"];

I would then go ahead and extract the needed parts and make requests against the database. This may seem a very very dumb question, but I do not know how to look this up on google (I tried...) but how exactly am I going to handle the link ?

I try to explain it step-by-step:

User clicks on article title -> the page reloads with new uri --> Where am I supposed to handle this new uri and how ? If the request path looked like this:

index.php?title=some-random-article-title

I would do it in the index.php and read the $_GET array and process it accordingly. But how do I do it with the proposed structure at the beginning of this question ?

 Answers

43

You will need a few things:

  1. Setup an .htaccess to redirect all request to your main file which will handle all that, something like:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    The above will redirect all request of non-existent files and folder to your index.php

  2. Now you want to handle the URL Path so you can use the PHP variable $_SERVER['REQUEST_URI'] as you have mentioned.

  3. From there is pretty much parse the result of it to extract the information you want, you could use one of the functions parse_url or pathinfo or explode, to do so.

Using parse_url which is probably the most indicated way of doing this:

$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "https" : "http";
$url = $s . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
var_dump(parse_url($url));

Output:

["scheme"] => string(4) "http" 
["host"]   => string(10) "domain.com" 
["path"]   => string(36) "/health/2013/08/25/some-random-title" 
["query"]  => string(17) "with=query-string"

So parse_url can easily break down the current URL as you can see.

For example using pathinfo:

$path_parts = pathinfo($_SERVER['REQUEST_URI']);

$path_parts['dirname'] would return /health/2013/08/25/

$path_parts['basename'] would return some-random-title and if it had an extension it would return some-random-title.html

$path_parts['extension'] would return empty and if it had an extension it would return .html

$path_parts['filename'] would return some-random-title and if it had an extension it would return some-random-title.html

Using explode something like this:

$parts = explode('/', $path);
foreach ($parts as $part)
    echo $part, "n";

Output:

health
2013
08
25
some-random-title.php

Of course these are just examples of how you could read it.

You could also use .htaccess to make specific rules instead of handling everything from one file, for example:

RewriteRule ^([^/]+)/([0-9]+)/([0-9]+)/([0-9]+)/([^/]+)/?$ blog.php?category=$1&date=$2-$3-$4&title=$5 [L]

Basically the above would break down the URL path and internally redirect it to your file blog.php with the proper parameters, so using your URL sample it would redirect to:

http://www.mysite.com/blog.php?category=health&date=2013-08-25&title=some-random-title

However on the client browser the URL would remain the same:

http://www.mysite.com/health/2013/08/25/some-random-title

There are also other functions that might come handy into this for example parse_url, pathinfo like I have mentioned early, server variables, etc...

Wednesday, March 31, 2021
 
mattltm
answered 8 Months ago
62

You're using it wrong:

 mail($to, $subject, $body, $from, $headers)

The mail() function does not have a $from parameter. You ought to throw it into $headers as well.

Saturday, May 29, 2021
 
Shobit
answered 5 Months ago
52

I figured it out. This is not a htaccess or url redirection issue. It is JavaScript issue. # is used for jquery. My function was not working properly.

So, it was redirection to localhost. I found this link which help me understand what is going wrong. What does <a href="#" class="view"> mean?

Here is why it was not directing. I used # in jquery. Like below.

$(document).ready(function(){ 
$("#").click(function(){
    alert("Hello!");
});

$("#") isn't a valid selector. So, i will have to use anchor tags.

Below is the working code.

$(document).ready(function(){ 

    $(".vote").click(function(event) {
    event.preventDefault();
    alert("Hello!");
});

event.preventDefault() used because i am using

Hope this help someone.

I got help to find out the issue by DelightedD0D. You can see the complete answer on this link.

bind click event to anchor tags on my page

Saturday, May 29, 2021
 
PHLAK
answered 5 Months ago
13

I wrote this yesterday:

decltype(x), where x is a structured binding, names the referenced type of that structured binding. In the tuple-like case, this is the type returned by std::tuple_element, which may not be a reference even though the structured binding itself is in fact always a reference in this case. This effectively emulates the behavior of binding to a struct whose non-static data members have the types returned by tuple_element, with the referenceness of the binding itself being a mere implementation detail.

Friday, June 18, 2021
 
jonboy
answered 5 Months ago
97

You can either Base64-encode it and send it as a string in a JSON message, or you can POST or PUT the binary as a separate resource and refer to it by ID or URL in the JSON message. The latter approach is a kind of out-of-band data channel that is quite common in XML-based protocols (e.g., voice chat using XMPP).

You could even quite easily support a hybrid model, whereby:

  1. A small image is sent as {"base64":"OGZmNjJmOWNhYzFlODE0NDBjYmYzNjhjYz..."};
  2. A large image is uploaded as a reference, {"ref":"http://myserver.com/bits/E4304205-29B7-48EE-A359-74250E19EFC4"}.

To avoid the double-POST needed for externally referenced binaries, you could design some protocol that allows JSON and binary stuff to be mixed in a single transfer. But the incremental gain is unlikely to adequately reward that level of effort.

Finally, from a design perspective, stick to the simple solution until it becomes a problem.

Wednesday, August 4, 2021
 
123r789
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share