Forum Moderators: coopster

Message Too Old, No Replies

Php and htacces

         

mikemcs

2:45 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



I am having some problems with htaccess and could use some help. I have read the Introduction to mod_rewrite post and I think I need a bit more help. I have a URL that looks like this: www.mysite.com/index.php?Detail=66 I want to redirect to www.mysite.com/index.php?Detail=88.

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

jdMorgan

3:38 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mikemcs,

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]

HTH,
Jim

mikemcs

4:42 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



jdMorgan, Thank you for you post. I did try this but it doesn’t seem to be working. I did modify it a little to handle the actual URL I want to change.

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?

jdMorgan

6:00 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The whole query string will be in the {QUERY_STRING} variable, so multiple variables should not hurt you.

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

jdMorgan

6:25 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mikemcs,

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 need to do further rewites to the URL after the one you've specified and before a file is served, omit the [L] flag, using only [R=301] if you want to see the redirect in the browser.

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

rfontaine

6:43 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



You could accomplish the redirect without .htaccess if you have to:

In the code on your web page - must be at the top - you could do something like

if detail=66
then
redirect(http://xyz.com?detail=88);

Of course this is just the logic to the script.

mikemcs

7:05 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



jdMorgan,
I stiky mailed you I hope this was ok.