Forum Moderators: phranque

Message Too Old, No Replies

Query string not working

Changing filename but maintaining query string

         

batface

8:04 am on Jul 24, 2011 (gmt 0)

10+ Year Member



I've been trying to amend the filename of a URL leaving the query string in tact but it is not working. Can someone point me in the right direction.

.../video/video.php?vid=029252fcc
to
.../video/myvideo.php?vid=029252fcc

I've tried this but nothing is happening. I'm using [^&]+ to capure any characters other than &:

RewriteCond %{QUERY_STRING} ^(vid=[^&]+)
RewriteRule ^video/video\.php$ video/myvideo.php?%1 [R=301,L]

I also tried the explicit
RewriteCond %{QUERY_STRING} ^(vid=029252fcc)
RewriteRule ^video/video\.php$ video/myvideo.php?%1 [R=301,L]

but no change. What am I doing wrong? :-(

Rewrite works fine for other rules.


Mick

lucy24

8:30 am on Jul 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't need to do anything at all :) By default, a rewrite leaves the query string absolutely untouched and quietly reappends it after doing its stuff. If you take care of the main url, the query string will take care of itself.

batface

9:07 am on Jul 24, 2011 (gmt 0)

10+ Year Member



but
RewriteRule ^video/video\.php$ video/myvideo.php [R=301,L]

does not work. :-(

g1smd

4:00 pm on Jul 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In what way does it "not work"?

You'll need
RewriteEngine On
and you may or may not need
Options +FollowSymLinks


You should request
example.com/video/video.php?<something>


The server will send a 301 response. You can check this using the Live HTTP Headers extension for Firefox.

The browser will make a new request, this time for
example.com/video/myvideo.php?<something>


The server will then attempt to retrieve content from the server path
/var/www/yoursite/myvideo.php
, passing the appended query string data to that resource. It will send a 404 only if the myvideo.php file does not exist.

You should add the domain name to your redirect target.

The redirect code should be in the root .htaccess file.

batface

6:42 pm on Jul 24, 2011 (gmt 0)

10+ Year Member



.../video/video.php?vid=029252fcc is a 404 page due to a SEF plugin in Joomla. There are too many pages to do anything other than redirect to the new filename which does exist:
.../video/myvideo.php?vid=029252fcc

I have many rewrites already in the .htaccess and they are working well, none have query strings.

I'm a little confused because <something> must be the query string right? So if I add the line
RewriteCond %{QUERY_STRING} ^(vid=[^&]+)

I thought I was "giving life to" (can't think of the right phrase) the query string so it will be referenced and added to the target URL when I use %1 as in my first post.

But then Lucy24 says that I dont need to worry about the query string as it will reappend itself on the target.

So on here I have read this on a couple of threads, and also looked at threads where the query string RewriteCond is used.

I have used both but neither seem to do anything. That is the same URL in the browser remains, which returns a 404 when using Fiddler or HTTP Headers (as well as in the browser).

So now I am confused as to whether i'm using the wrong syntax or something else is wrong. I'm hoping it is that the penny hasn't quite dropped.

lucy24

8:03 pm on Jul 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{QUERY_STRING} blahblah

is needed in these situations:

#1 when you want to apply the rewrite only to pages that have a query string, or that don't have one, or whose query string contains a particular element

#2 when you need to capture part of the query string in order to replace the existing one.

In the rewrite, these are the possibilities for the "target":

...blahblah without question mark = don't change the query string (the default)
...blahblah? with trailing question mark = delete the entire query string (this is where you need to make sure there was one in the first place to avoid infinite loops if you're not concurrently changing something else)
...blahblah?morestuff = replace existing query string with "morestuff" (which might be either brand-new text, or text captured as part of the RewriteCond)
...blahblah?morestuff [QSA] = add the new stuff (Query String Append) to your existing query string

batface

3:58 pm on Jul 25, 2011 (gmt 0)

10+ Year Member



Oh dear, before I go down the end of the garden and feed myself to the birds please help.

I understand that I need scenario 1, so why is

RewriteCond %{QUERY_STRING} ^(vid=[^&]+)
RewriteRule ^video/video\.php$ video/myvideo.php%1 [R=301,L]

not working? I just want to change the filename! aaarggh!

lucy24

4:37 pm on Jul 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, yes, it's hard to believe you don't have to do anything.

RewriteCond %{QUERY_STRING} ^vid=
RewriteRule ^video/video\.php$ video/myvideo.php [R=301,L]

"Apply the rule only to requests whose query begins in 'vid='".

"Redirect from file 'video.php' to file 'myvideo.php' without doing anything to the query string."

As written, your code takes the whole query string and appends it to "php", resulting in gibberish. If you did want to capture and reuse the query string, you would have to say ...myvideo.php?%1 (option #3 under "target") with question mark. But since you're reusing the whole thing, you don't need to say anything about it.

batface

6:13 pm on Jul 25, 2011 (gmt 0)

10+ Year Member



If only it worked! :-(

I tried both

RewriteCond %{QUERY_STRING} ^vid=
RewriteRule ^video/video\.php$ video/myvideo.php [R=301,L]

and

RewriteCond %{QUERY_STRING} ^vid=
RewriteRule ^video/video\.php$ video/myvideo.php?%1 [R=301,L]

What could be going wrong?

g1smd

6:27 pm on Jul 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is vid= the FIRST parameter after the question mark in the original request?

The ^ character means ^begins with.

You should also add the protocol and domain name to the redirect target.

batface

6:39 pm on Jul 25, 2011 (gmt 0)

10+ Year Member



Yes vid= comes immediately after the ?

RewriteCond %{QUERY_STRING} ^vid=
RewriteRule ^video/video\.php$ http://www.example.com/video/myvideo.php [R=301,L]

makes no difference.

batface

6:55 am on Jul 26, 2011 (gmt 0)

10+ Year Member



RewriteCond %{QUERY_STRING} ^vid=
RewriteRule ^video.php$ http://www.example.com/video/myvideo.php [R=301,L]

works! :-)

There is a seperate htaccess in the video directory.

This has raised 2 questions.

1. Why doesn't the htaccess in the root take priority, where is it set as to which htaccess does what?

2. Why in this htaccess escaping the . is not necessary?

lucy24

7:23 am on Jul 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Each successive htaccess comes "on top of" the one before. They are processed in order, from outermost to innermost.

2. In RegEx-speak, the period . means "any one character" -- including the period itself. Now, if you had something named video/php or videozphp or (et cetera) ... then you would be in trouble.