Forum Moderators: phranque

Message Too Old, No Replies

Using mod rewrite to replace word in query string

         

jepler

3:13 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



Hello,

I have what I think is a simple issue. I'd like to replace a page's file name in a query string with a new name. For example,

http: //mydomain.com/dir/page_1.php?filename=something&file=something&etc
http: //mydomain.com/dir/new_name.php?filename=something&file=something&etc

The following placed in .htaccess is obviously wrong. I think I might need to add a condition but not sure?


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^dir/[i]page_1\.php[/i]$ http: //mydomain.com/dir/[b]new_name\.php[/b]?$ [R=301,L]

Thanks in advance for any help you can provide.

jdMorgan

5:38 pm on Mar 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The page's name does not appear in the query string posted above, so this is not clear.

What, specifically, is wrong about your code's behaviour? How did you test, and what URL did you type in? What did you expect to happen? What were the actual results? How did those results differ from your expectations?

Note also that appending a question mark to your substitution path in the RewriteRule will remove the query string parameters from that new path.

Do you want to externally redirect the client, or do you want to change the server filepath associated with a request for that pages URL? Note that a filepath and a URL are two completely-different things, and that a filepath is "associated" with a URL by the server. mod_rewrite can be used to generate an external redirect when the client is attempting to use an obsolete URL, or it can change the default URL-to-filename mapping internal to the server; Which of these it does is up to you and the RewriteRule syntax you specify.

A redirect sends an HTTP response to the client (e.g. browser or search engine robot) saying, "The URL you just requested is wrong or obsolete. Please ask for that resource again using this new URL." An internal rewrite simply tells the server, "If you get a request for this URL, server the content using that new filepath."

Jim

jepler

10:29 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



sorry, I'll try to be more clear. The name of the query page has changed from page_1.php to new_name.php. Consequently, I want outdated URLs with the old address to redirect to the newer page. So to answer your question, I guess I want an external redirect NOT an internal rewrite.

Maybe I'm making this more difficult than what it is? I added the aforementioned code into the .htaccess file, tried typing the old url, but didn't see the browser redirect to the new URL. That was my test.

I'm using .htaccess to redirect other URLs in my site and they seem to work just fine. For example, the following is working:

Redirect 301 /page.html [mydomain.com...]

**UPDATE:
I was making it harder than it was. I used the same convention as before and it worked. I think I was getting thrown off by the query string. I see now that the variables in the query string will be retained even if they aren't present in the .htaccess directive. Sorry about that...

[edited by: jepler at 10:36 pm (utc) on Mar. 27, 2009]

g1smd

10:33 pm on Mar 27, 2009 (gmt 0)

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



is it just the filename as loaded on the server that has changed?

If so, then the URL that people use does not have to change...

Why has the filename changed?

What were you hoping to achieve?

To be clear, filenames and URLs are not the same. They are merely associated. Just because one changes, the other does not have to.

jepler

10:42 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



thanks to all who replied. I wanted the URL address to change, and by adding the following it did the trick:

Redirect 301 /dir/page_1.php http: //mydomain.com/dir/new_name.php

I was making it more difficult than it needed to be.

g1smd

10:45 pm on Mar 27, 2009 (gmt 0)

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



Yes, but why did you want a new URL for the content?

jepler

10:53 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



well, to be honest it was for cosmetic reasons. The original page had a name that I didn't like, so I changed the page's name to something shorter and more appropriate.

I've used mod rewrite to rewrite pretty URLs before, but for some reason this approach wasn't working on this particular site because of its heavily dependent use of variables present in the query string; variables that I need to include in the URL or some content doesn't show on the page. Things were breaking left and write so I decided to go with this approach which works for me.