0

I feel a little stupid asking this - like i should be able to find out by googling - but i haven't found any really good, controversial ideas on this topic.

I am thinking about rewriting my URLs from dynamic http://www.example.com/?section=news&news_id=42 to static http://www.example.com/news/42/awesome-news-item-title - i have already found out how to solve this and like the result a lot. Now, before implementing this on my site, i'd love to have some neutral input on Browser performance.

All the sources i found said static URLs are more SEO-friendly (except for this google webmaster central blogpost) - they sure as hell are more user-friendly regarding readability.

It's only that I had the impression of the website loading waaay longer using the rewrite.

RewriteRule ^news/([0-9]+)(\/[a-z-]+)?$ /index.php?section=news&news_id=$1&title=$2

So, my bunch of questions is:

  • How does mod_rewrite affect browser performance?
  • Would you rewrite the URLs anyway?
  • Is there a better way to do it than the one I used?
  • I would go with this answer for url-encoding my news-titles. Agreed? Better suggestions?
  • Is there anything I really have to pay attention to? (Apart from checking if the title given in the URL matches the title of my news to avoid things like http://www.example.com/news/42/example-com-is-stupid)
Community
  • 1
  • 1
Matthias Schmidt
  • 585
  • 1
  • 7
  • 25

2 Answers2

1

Honestly have no clue on performance for mod_rewrite, but I can tell you:

  1. It won't affect "browser performance" because it happens on the server.
  2. Apache hints that there isn't much of a performance hit, because it specifies that This ruleset relies on HostNameLookups being set on, which can be a significant performance hit. for "Redirecting to Geographically Distributed Servers" (and no basic rewrite types).
  3. The SEO/clean aspects of static URLs would greatly outweigh any performance hits, and this is why people continue to use them.

As a side note, a lot of CRMs redirect all URLs to a index.php and use a URL router (see source code from a project like klein.php or Slim) to handle all requests. If you mainly have dynamic content for something simple like /news, this may not be necessary for the scope of your project. But if you start to create rewrite rules for multiple different URLs (like news, categories, authors), it will definitely be easier and more efficient to route all requests to index.php and use PHP to see what URL was accessed and what function to "route the request" to.


In response to your comment:

My (and a common) rewrite rule is something like the following:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L,QSA]

This redirects all requests that are not a file or a directory to index.php. This means anytime tries to access files that exist in your repository, it will serve up the files directly (so images, styles, scripts, and even PHP scripts in your public directory). In other words, cacheable content is not affected and you can use index.php and $_SERVER['REQUEST_URI'] to route URL calls to different classes and functions.

Sam
  • 20,096
  • 2
  • 45
  • 71
  • thank you for your answer, indeed i use index.php to handle all requests. regarding browser performance I had the impression that some potentially cacheable content like images and css was reloaded with the rewritten static URL while with the good ol' dynamic one it wasn't - although i set 1 year expires rules to those files... maybe i should look at this again. – Matthias Schmidt Apr 03 '14 at 15:51
  • 2
    If you want to be able to access $_GET requests in the url, the RewriteRule would have to include [L,QSA] instead of just [L] – Rob Apr 03 '14 at 16:16
1

Not sure about performance but here is an example similar to what I use. I create the url title as a record in the database and would pull the record by title and not id. That way if someone wants to try and manipulate the title, the request would be invalid. Plus, it helps with SEO.

<?php

function route($path_position) {
  $url_path = $page_path = explode("/",$_SERVER['REQUEST_URI']);
  return filter_var($url_path[$path_position],FILTER_SANITIZE_URL);
}

$section = route(1);
$news_id = route(2);
$url_news_title = route(3);

$query = "SELECT * FROM news_table WHERE url_news_title = :url_news_title";
echo "/" . $section . "/" . $news_id . "/" . $url_news_title;

I use the following .htaccess file with this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Rob
  • 1,840
  • 2
  • 12
  • 19
  • that sounds really interesting - I thought about something like this as well but hesitated since the titles have to be unique then, right? how do you handle duplicates? add a number? maybe add the date? – Matthias Schmidt Apr 03 '14 at 15:55
  • 1
    @Mazzekazze you can have a check on-`INSERT` that will add a '-2' to the URL title, if the URL title would be a duplicate. – Sam Apr 03 '14 at 15:58
  • 1
    yes, the title would have to be unique, however, you could also match the news id: $query = "SELECT * FROM news_table WHERE id=:id AND url_title = :url_title – Rob Apr 03 '14 at 16:11