Forum Moderators: coopster
1. Can I do this with just .htaccess?
2. Can htaccess handle variables or do I also need mod_rewrite to do this?
3. I control the server the page is on. Do I have to install something to obtain mod_rewrite or is it a switch in my php.ini file?
my .htaccess file looks like this
Redirect permanent /index.php?Detail=66 h**p://www.-mysite.com/index.php?Detail=88
and sits in my www root directory.
Thank you
mod_alias, the module that includes Redirect permanent, doesn't handle query strings.
You'll need to use mod_rewrite, and in an .htaccess context, you'll also need to handle the query string pattern separately in a RewriteCond directive. (This is because the query string has already been stripped from the request and moved to an environment variable by the time the API phase where .htccess is processed is reached.)
Here's an example using your posted URLs:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Detail=66$
RewriteRule ^index\.php$ /index.php?Detail=88 [L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Detail=News&Department=66$
RewriteRule ^index\.php$ /index.php?Detail=News&Department=88 [L]
Am I breaking it by using more than 1 variable? Another thing I just thought of is that the index.php part of the URL never shows in the address bar. Would this mean a change to the RewriteRule?
It's not clear from your post... Is your version not working for you?
You may want to try putting the "?" into the RewriteCond pattern - it's been awhile since I played with this aspect of mod_rewrite, so I don't recall if it needs to be included.
For purposes of experimentation, you can use "fake" pages and querystrings, and redirect from those to a known page. This will allow you to experiment without affecting users on your site - unless you have a syntax error in mod_rewrite, which will cause a server error and give everyone a 500-Server Error. :(
If I get some time, I'll go do just that, and post again. I've used this technique before, but I no longer have access to that site's .htaccess file, so I can't go grab the working code.
If you want the new URL to show in the browser address bar, change the RewriteRule flag from [L] to [R=301,L]. This forces an external redirect, rather than a transparent (server-internal) redirect.
Jim
OK, I have tested the following code, and it works.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^ib=fubar&id=me$
RewriteRule ^common/user\.html$ /users/process.pl?id=jdm&pg=welcome [R,L]
If you're still having trouble, please post more detail about what happens when you try the rewrite - I can't tell where the problem might be, since the above works just fine for me. Take a look at your site error log, and see if there's anything logged in there that's related to these rewrites.
Jim