mod_rewrite question

  • kenw232
  • Born
  • Born
  • No Avatar
  • Joined: Feb 26, 2009
  • Posts: 2
  • Status: Offline

Post February 26th, 2009, 12:19 am

I have this in my .htaccess.

Code: [ Select ]
Options +Indexes +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^longurl-([A-Za-z0-9_]+).html$ /index.html?longurlstring=$1 [L]
  1. Options +Indexes +FollowSymlinks
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteRule ^longurl-([A-Za-z0-9_]+).html$ /index.html?longurlstring=$1 [L]


This works, but why?

If I goto "http://domain.com/longurl-test.html" then its fine and comes up like expected.

But if I goto "http://domain.com/longurl-test.html?something=data" it still works! Why - I specifically put a $ after "longurl-([A-Za-z0-9_]+).html" meaning this is the end.

Therefor "http://domain.com/longurl-test.html?something=data"
IS NOT
Code: [ Select ]
^longurl-([A-Za-z0-9_]+).html$


Why does it match? The "?something=data" should make it not match I would think.

Thanks for any help.
Moderator Remark: ; Added [code] tags
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 26th, 2009, 12:19 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 26th, 2009, 9:26 am

The query string is not considered part of the URL by mod_rewrite, but is still an accessible enviroment variable. This used to confuse me at first too.

If you want to make sure the query string is empty, you'll have to use RewriteCond as well.

Code: [ Select ]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^longurl-([A-Za-z0-9_]+).html$ /index.html?longurlstring=$1 [L]
  1. RewriteCond %{QUERY_STRING} ^$
  2. RewriteRule ^longurl-([A-Za-z0-9_]+).html$ /index.html?longurlstring=$1 [L]


If you want to preserve the passed query string while appending your own you can use the QSA flag with RewriteRule.
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 14 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.