A Better Way to Do SEO Friendly URLs with Apache

I currently have a Ubuntu 10.04 server with Apache, PHP, and MySQL. I'm using it to host, among other things, a blog with a custom CMS. I've been using .htaccess (generated by the CMS when new blog posts are added) and mod_rewrite to create SEO friendly URLs for the blog posts.

ie

mydomain.com/blog/blog-post-title/ is actually mydomain.com/BlogPost.php?ID=(Number) but the user never knows

Since enabling .htaccess and using mod_rewrite for this, I feel like my server is a little slower.

So two questions:

1. Is that possible? Can enabling .htaccess have that much of an affect? Or am I likely imagining things?

2. (The main question): Is there a better way to do SEO friendly URLs?

I'd appreciate any advice you might have.

7 Replies

Yes .htaccess can cause things to slow down, since it is checked on every request. Copy the rules from .htaccess to your virutal host, restart apache, and then remove the htaccess.

mod_rewrite is still the best way (as far as I know) to do seo friendly urls.

Caveat: I'm not really an expert in Apache tweaking, so take my advice with a grain of salt.

As obs pointed out, .htaccess files get parsed once per request. If your CMS is creating one rewrite rule per blog post, your .htaccess file is perpetually growing. Needless to say, that's a recipe for poor performance that will only get worse.

If you use mod_rewrite's RewriteMap directive, then you should be able to speed up the handling of requests. RewriteMap goes in your server or virtual host config, and it's smart enough to cache the mapping file until the timestamp changes. Just tweak your CMS to update the map file instead of the .htaccess file, and then use a simple RewriteRule that relies on the map to do the post title -> post id conversion.

Thanks for the suggestions. I'll try them out.

@Erasmus Darwin:

As obs pointed out, .htaccess files get parsed once per request. If your CMS is creating one rewrite rule per blog post, your .htaccess file is perpetually growing. Needless to say, that's a recipe for poor performance that will only get worse.

CMS don't usually do this, normally they just redirect back to some index file i.e. wordpress and drupal do this. It's also probably a bad idea if your apps can write to the .htaccess file since if there's a problem in the app someone could cause headaches by putting a "deny all" into it.

> normally they just redirect back to some index file i.e. wordpress and drupal do this

@obs How does Wordpress and Drupal do this without an htaccess file?

Well they both create a .htaccess file, so all you need to do is copy the contents of that file into the virtualhost file for that site then remove the .htaccess file.

Since drupal/wordpress only create the files when asked to by the admin from the web interface you will only need to update your vhost if you ever change your settings (which most people don't).

Lightbulb moment!

OK thank you! You've given me the help I need to figure out the rest! Goodbye .htaccess.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct