Forum Moderators: phranque

Message Too Old, No Replies

Complex .htaccess Rewrite Rule

         

skicrave

8:36 pm on May 10, 2005 (gmt 0)

10+ Year Member



Sorry to bring up another htaccess question, but I haven't been able to solve this on my own.

I would like to redirect all requests to domain/nice/index.php, domain/nice/index.php?page=browse&cat=6 and domain/nice/ to domain/browse.php, BUT not redirect any other query string combinations.

How can I test to catch the one query string redirection, but exclude all other?

Thanks,

Jason

setme

9:16 pm on May 10, 2005 (gmt 0)

10+ Year Member



I would like to redirect all requests to domain/nice/index.php, domain/nice/index.php?page=browse&cat=6 and domain/nice/ to domain/browse.php

I'm farily new to rewriterule, but have you tried

RewriteEngine on
RewriteRule ^/nice/$ /domain/browse.php [R]

And then having a something in index.php like

if ($_GET['page'] =="browse") && ($_GET['cat'] =="6")
header ("Location: [domain...]
exit;

jdMorgan

9:28 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jason,

Welcome to WebmasterWorld!

Here's one way to do it:


RewriteCond %{REQUEST_URI)<->%{QUERY_STRING} ^/nice/index\.php<->page=browse&cat=6$ [OR]
RewriteCond %{REQUEST_URI}<->%{QUERY_STRING} ^/nice/index\.php<->$ [OR]
RewriteCond %{REQUEST_URI}<->%{QUERY_STRING} ^/nice/<->$
RewriteRule ^nice/(index\.php)?$ http://www.example.com/browse.php [R=301,L]

Jim

skicrave

10:12 pm on May 10, 2005 (gmt 0)

10+ Year Member



setme,

Thanks for the creative solution. That should work, but I would like to handle the redirect in Apache to prevent problems down the road with file changes.

Jim-

Thanks for the kind welcome, the information here is impressive, and everyone is so willing to help. I will definitely be spending some more time.

The code you suggested handles every situation except for when the user simply enters domain/nice/

Is there a simply way to redirect that as well?

Thanks,

Jason

jdMorgan

11:49 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The code you suggested handles every situation except for when the user simply enters domain/nice/

I assume this comment is not a result of testing. The code should handle that situation as well, since the "index.php" in the rewriterule is optional - it is surrounded by "()?" where the question mark makes the parenthesized subexpression optional.

Either that, or I did not understand your goal.

Jim

skicrave

4:43 pm on May 11, 2005 (gmt 0)

10+ Year Member



Jim-

Sorry, I didn't mean to make my response sound hostile.

Here is what I have in my .htaccess file right now:

RewriteEngine on 
RewriteCond %{QUERY_STRING} ^page=browse&cat=6
RewriteRule /* http://www.domain.com/browse.php [R,L]

This works for all cases except when index.php is not included in the URL.

I did test your code, and placing it in the root directory didn't accomplish anything. Your way seems like a better way to do things, but it's not working in my application for some reason.

Thanks,

Jason

jd01

12:39 am on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jason,

I am not sure I understand your code...

RewriteRule /* [domain.com...] [R,L]

Since the leading / is stripped by Apache, when the rule is passed, this will only match a request that has 0 or 2 /'s, and is for the directory you are currently in. The fact that you also do not have a firm beginning ^ and ending $ to your rule could also cause issues.

I think you would be much more productive, and sound, to use Jim's example and make the / optional:

RewriteRule ^nice/?(index\.php)?$ [example.com...] [R=301,L]

Justin

BTW My guess would be if the example Jim gave is not working, there is some small adaptation necessary to make the match 'perfect' for the specific case you are looking for.