0

I was spoon fed this htaccess file from Gumbo and I am grateful for it as I learnt a lot. However, I made some changes and reverted back and forth and managed to make some small changes again, it works 80% but there is a case when it doesn't work:

If I type in http://www.example.com/view.php?t=45re it rewrites successfully but it does this in the URL http://www.example.com/**?t=**45re I can't see how that is happening. Anymore help greatly appreciated.

# REWRITE DEFAULTS
RewriteEngine On
RewriteBase /

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^view\.php$ / [L,R=301]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]
Abs
  • 56,052
  • 101
  • 275
  • 409

2 Answers2

1

Not knowing your case nor what Gumbo told you, from what I can gather from your question (ie you want that the query string is not passed on when externally redirecting), you could either

RewriteRule ^view\.php$ /? [L,R=301]

note the extra ?, or

RewriteRule ^view\.php$ / [L] 

removing the external redirect and use an internal one.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • I tried both. The first one, will redirect to the homepage with nothing after the "/". The second will also redirect to the homepage but still showing the page and query string "/view.php?t=3ew". Gumbo, I think had a "/%2" - I wasn't sure what it was so I made some changes and I made things worse some how! – Abs Jul 12 '09 at 00:10
  • Yes, don't delete parts of what's working just because you don't understsand them!!! You could've asked "What does the %2 mean here?" before deleting it! :P – Vinko Vrsalovic Jul 12 '09 at 07:39
1

I think you mean this post. And I have to admit that I forgot the empty query in the substitution to override it, like Vinko already mentioned.

So try this:

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^view\.php$ /%2? [L,R=301]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]

The %2 is a backreference to the match of the second grouping of the pattern of the corresponding RewriteCond directive. So %2 will in this case hold the match of ([^&]+) in ^([^&]*&)*t=([^&]+)&?.*$.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Heh. I had it alright, but I deleted the parts I don't understand. I won't provide links to the original question either. :-) – Vinko Vrsalovic Jul 12 '09 at 07:38
  • In my defence, I am a noob just like both of you were at some point. Btw, I didn't delete because I didn't understand, I deleted it because I was trying other things that I thought might work. :) – Abs Jul 12 '09 at 16:57