Forum Moderators: phranque
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.
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
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]
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.
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.