Skip to content
Advertisement

mod_rewrite Clean URL rewriting

I’m trying to write clean url’s for my website. I’m a rookie at this so forgive me. My .htaccess file currently looks like this:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)$ something.php?query=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ something.php?query=$1

The first rule seems to work. For example website.com/good-looking-query-string does in fact rewrite to website.com/something.php?query=ugly+looking+query+string

The second rule is where I’m having a problem. I can’t get that trailing slash to work. For example website.com/good-looking-query-string/ appears to pull up the page but without any CSS rules applied. I noticed all the links end up appended to the query string also. For example the link back to index.php ends up like website.com/good-looking-query-string/index.php

I need to get that trailing slash to work. What in the world am I doing wrong?

Advertisement

Answer

You can combine two line in one line:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1

In this case / will be optional

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement