mod_rewrite question

  • kenw232
  • Born
  • Born
  • kenw232
  • Posts: 2

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Sledgehammer
  • Genius
  • User avatar
  • Posts: 13496
  • Loc: Florida

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 9 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.