.htaccess - RewriteRules working, but browser address bar displaying full (unfriendly) URL

Posted by axol on Stack Overflow See other posts from Stack Overflow or by axol
Published on 2010-04-09T02:13:35Z Indexed on 2010/04/09 7:33 UTC
Read the original article Hit count: 801

Hey there,

Haven't been able to find a solution to this around the net or these forums - apologise if I've missed something!

My .htaccess RewriteRules are working well - have search-engine and user -friendly links in my web pages, and unfriendly database URLs running hidden in the background.

Except when I added a RewriteRule to add "www." to the front of URLs if the user didn't enter it - to ensure only one appears in search engines. Here's what now happening, and I can't figure out why!

My friendly URL structure for content is like this, and the database query string uses the first "importantword":

www.example.com/importantword-nonimportantword/

.htaccess snippet:

Options +FollowSymLinks
Options -Indexes

RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteBase /

RewriteRule ^/$ index.php [L]

RewriteRule ^(.*)-(.*)/overview/$ detail.php?categoryID=$1 [L]

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L]

What's happening since I added the last 2 lines:

CASE 1: user types (or clicks) www.example.com/honda-vehicle/overview/
- Works correctly
- They are taken to the correct page and the browser URL bar says:

www.example.com/honda-vehicles/overview/

CASE 2: user types example.com
- Works correctly
- They are taken to www.example.com and the browser URL bar says:

www.example.com

CASE 3: user types (or clicks) example.com/honda-vehicles/overview/ i.e. without the prefix "www"
- Does NOT work correctly
- They are taken to the right page, but the browser URL bar displays the unfriendly URL:

www.example.com/detail.php?categoryID=honda 

I figure there's some issue with the order of the RewriteRules, but it's doing my head in trying to logically step through it and figure it out!

Any assistance at all or pointers would be most appreciated!

© Stack Overflow or respective owner

Related posts about .htaccess

Related posts about rewriterule