fry2010 Posted August 28, 2011 Share Posted August 28, 2011 I wish to achieve the following: Redirect www.mysite.com/dir1/dir2 to www.mysite.com?page=$1 At the same time see if there is a specific directory called 'specialpage' and then redirect to www.mysite.com?page=$1&special=$2 I can get the first rewrite to work, but when I try to see if special page is there and pass as a second parameter it stops working if that special parameter does not exist. I know the reason why it does not work, but I dont know what the solution is. Here is what I have so far: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^/www.mysite.com/(.*)$ RewriteCond %{REQUEST_URI} ^/(.*)/page([0-9]*)$ ##This is the special page RewriteRule ^(.*)$ /www.mysite.com/index.php?loadPage=$1&special=$2 [L,QSA] The reason it wont work is because when it gets to the second rewrite condition, if it does not match that condition then it will not perform the rewrite rule. I need it to work so that if it does not match the second condition then it will still perform the rewrite rule but leave out the second GET variable. Hope this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/ Share on other sites More sharing options...
gizmola Posted August 29, 2011 Share Posted August 29, 2011 You can use the OR flag RewriteCond %{REQUEST_URI} ^/www.mysite.com/(.*)$ [OR] RewriteCond %{REQUEST_URI} ^/(.*)/page([0-9]*)$ ##This is the special page Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263003 Share on other sites More sharing options...
fry2010 Posted August 29, 2011 Author Share Posted August 29, 2011 thanks, thats good to know, but not what I needed it to do. I probably havnt explained very well. Basically I want the first condition to rewrite to the GET variable '$1', and then IF the second condition is also met, then provide that as the second GET variable '$2'. If it does not find the second condition then still perform the rewrite and pass the first variable. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263182 Share on other sites More sharing options...
gizmola Posted August 29, 2011 Share Posted August 29, 2011 Ok, you want 2 different RewriteCond/Rewrite Rule pairs. Start with the specific one for the upload. When that is matched, use your existing rule. If it is not matched, processing will fall through to the 2nd Condition/RewriteRule. In your 2nd rewrite rule, just omit the &special=$2 portion. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263185 Share on other sites More sharing options...
fry2010 Posted August 30, 2011 Author Share Posted August 30, 2011 cheers gizmola, suppose that is the best way to go. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263605 Share on other sites More sharing options...
fry2010 Posted August 30, 2011 Author Share Posted August 30, 2011 actually I am having a problem trying to get that to work. RewriteCond %{REQUEST_URI} ^/(.*)/page([0-9]*)$ RewriteRule ^(.*)$ /www.mysite.com/index.php?special=$1 [QSA] RewriteCond %{REQUEST_URI} ^/www.mysite.com/(.*)$ RewriteRule ^(.*)$ /www.mysite.com/index.php?loadPage=$1 [L,QSA] I get an internal server error. Exceeds 10 internal redirects in error log. I have tried them the other way around but that will mean the second condition will never be matched because the first allows for all to be matched. So they need to go this way around. (Which I assume is why you said to use the specific one first...) Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263610 Share on other sites More sharing options...
cags Posted August 30, 2011 Share Posted August 30, 2011 Try adding the L flag to the first one, that way any pattern that matches shouldn't progress further through the file. The only way it would is if your rewritten path was a redirect (which it doesn't appear to be). Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263643 Share on other sites More sharing options...
fry2010 Posted August 31, 2011 Author Share Posted August 31, 2011 tried that, but still gives server error. Strange because when I place them the other way around there is no server error. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263826 Share on other sites More sharing options...
fry2010 Posted August 31, 2011 Author Share Posted August 31, 2011 I think it has something to do with having two conditions that can be treated the same. Because when i place two conditions exactly the same then it gives server error. example: RewriteCond %{REQUEST_URI} ^/www.mysite.com/(.*)(/*)$ RewriteRule ^(.*)$ /www.mysite.com/index.php?aff=$1 [QSA] RewriteCond %{REQUEST_URI} ^/www.mysite.com/(.*)(/*)$ RewriteRule ^(.*)$ /www.mysite.com/index.php?loadPage=$1 [QSA] Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263827 Share on other sites More sharing options...
cags Posted August 31, 2011 Share Posted August 31, 2011 I dont' really understand why you are using RewriteCond's for this anyway. They aren't required, it can be done purely with a RewriteRule, you are just forcing two checks. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263907 Share on other sites More sharing options...
fry2010 Posted August 31, 2011 Author Share Posted August 31, 2011 oh ok. Its just cause I dont fully understand mod rewrite. I will probably get stuck with it so might be back lol. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1263990 Share on other sites More sharing options...
cags Posted August 31, 2011 Share Posted August 31, 2011 RewriteRule cond rewrite [flags] The cond part is regular expression which is matched against REQUEST_URI (well to be accurate the part of REQUEST_URI that is relative to the current directory). If it matches and the preceeding RewriteConds match then the request is rewritten to the rewrite part. So in your case you are finding requests that match anything (.*) then matching it against a pattern, you could just move that pattern to the cond part (minus the beginning forward slash if it's going in .htaccess file). Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1264000 Share on other sites More sharing options...
fry2010 Posted September 1, 2011 Author Share Posted September 1, 2011 excellent thanks cags and giz, finally got it now, and a better solution too. Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1264171 Share on other sites More sharing options...
gizmola Posted September 1, 2011 Share Posted September 1, 2011 Cags +1! Quote Link to comment https://forums.phpfreaks.com/topic/245902-how-to-match-two-different-conditions-but-give-same-rewrite-rule/#findComment-1264457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.