Forum Moderators: phranque

Message Too Old, No Replies

Help with query string rewrite

htacces query_string rewrite url variables

         

mlmiddleton

8:44 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



I'm trying to rewrite a URL that has a query string on it, and despite finding a lot of information on this, I can't get it to work, can someone help me?

redirect:

http://www.example.com/file.php?x=123

to:

http://www.example.com/file/123

Here's what I'm trying:

RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^file.php(.*)$ /file/%1

I have further rules for the framework that I'm using after this:

RewriteCond $1 !^(index\.php¦css¦js¦images¦robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

The reason that I'm doing this is to preserve current links to /file.php?x=123, and have them redirect to the new site that I'm building that will have links of /file/123

I don't really speak this, and this is the only thing I need to do with this, can someone help me?

g1smd

11:56 pm on Jun 25, 2009 (gmt 0)

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



Do you want a "redirect" or a "rewrite" here? You used both words in your question. They are two different things.

Your code is for a rewrite. I think you need a redirect if your explanation of what you want to do is correct.

In that case, you'll need to add the domain name to the target URL and the [R=301,L] flags.

jdMorgan

12:14 am on Jun 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The overall purpose of this whole project hasn't been mentioned, nor what specifically "doesn't work."

If you are trying to achieve "search engine friendly URLs," there are three steps:

  1. Change the links on your pages to "/file/123" format.
  2. Internally rewrite /file/123 to /file.php?x=123
  3. Externally redirect direct client requests only for /file.php?x=123 back to /file/123

Jim

mlmiddleton

12:54 am on Jun 26, 2009 (gmt 0)

10+ Year Member



In the above list, it's #3. I already have search engine friendly URLs, so, if you go to /file/123, it renders the page just fine. However, for the previous version of this site, the page would have been /file.php?x=123. So, for existing bookmarks and links that are already out there that point to /file.php?x=123, I want to direct it to what's at /file/123, instead of page not found.

g1smd

1:18 am on Jun 26, 2009 (gmt 0)

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



My points, and Jim's #3 are what you need to do then...

jdMorgan

2:08 am on Jun 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To redirect only direct client requests (avoiding an infinite redirect/rewrite loop), test THE_REQUEST:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /file\.php\?x=([^\ ]*)\ HTTP/
RewriteRule ^file\.php$ http://www.example.com/file/%1? [R=301,L]

To clarify the RewriteCond pattern above, THE_REQUEST is the exact request string sent by the client (e.g. browser), and is visible in your raw server access logs. A typical request would look like:
GET /file.php?x=123 HTTP/1.1

Using this additional RewriteCond check prevents this rule from redirecting requests which have just been internally rewritten to the "file.php?x=123" form by your other rule, and so avoids a loop.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 2:59 am (utc) on June 26, 2009]

mlmiddleton

2:49 am on Jun 26, 2009 (gmt 0)

10+ Year Member



Thank you so much, that's exactly what I needed. I just added a ? on the end of the redirected URL to clear the existing query string:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /file\.php\?x=([^\ ]*)\ HTTP/
RewriteRule ^file\.php$ http://www.example.com/file/%1? [R=301,L]

Thank you again!

acimag

7:56 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



I dont get it when all those switch characters start...

I tried this and it works.... only little problems that a real brain could figure out...
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /\?p=([^\ ]*)\ HTTP/
RewriteRule ^$ [domain.com...] [R=301,L]

The only problem is it doesnt have the .html to the end. just leaves it in directory structure.

Next thing I realized after.

Most of them are ?p=blah then I noticed that SOME had ?p=blah&town=bla_g sometimes theres and underscore sometimes there isn't.

Any idea's?

g1smd

8:02 pm on Jun 26, 2009 (gmt 0)

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



See your other thread: [webmasterworld.com...]

acimag

8:50 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



thanks ;)