Jump to content

How to match two different conditions but give same rewrite rule


fry2010

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.