views:

36

answers:

2

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]
+1  A: 

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
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
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
+1  A: 

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=([^&]+)&?.*$.

Gumbo
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
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
It works great now. Maybe if I delete this... ;)
Abs