Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2011
    Location
    Berkshire UK
    Posts
    9

    Another mod_rewrite problem

    Hi,
    I have a php site in which different pages are called by a url like:
    Code:
    www.mysite.com/dir/index.php?p=21&x=12345679
    There may or may not be a number of other paramaters after the p paramater

    Temporarily I want to redirect a number of those pages to another site so that for example

    Code:
    index.php?p=21 goes to www.myothersite.com/index.html
    index.php?p=26 goes to www.myothersite.com/index.html
    index.php?p=28 goes to www.myothersite.com/index.html
    So I have tried to set a .htaccess file containing the following

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mysite.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.mysite.com$
    
    RewriteRule ^dir\/index\.php\?p=2[168].*$ http://www.myothersite.com/index.html [L,R=302]
    But this gives a 500 (internal server) error

    I am not very familiar with either mod_rewrite or regexes so I cannot see what I am doing wrong. Can anyone point out the errors.

    Thank you
    Richard

  2. #2
    Why do you have the [168]?

  3. #3
    Join Date
    Apr 2011
    Location
    Berkshire UK
    Posts
    9
    'Cause that matches 21, 26, or 28 and no other number ( I think)

  4. #4
    Join Date
    Aug 2005
    Location
    Durham, NC USA
    Posts
    195
    The only thing that I can see that might be causing a problem is that you've escaped the forward-slash for the directory. You don't need to do that, and it may be confusing the system.

    Also, if you don't have any other pages that use the p=21 arguments, then you can probably dispense with the whole prefix and just use something like

    RewriteRule ^(.*)p=2[168](.*) http://www.myothersite.com/index.html [L,R=302]

    Otherwise, if you need the directory and the index.php, you might try something like

    RewriteRule ^/dir/index.php.p=2[168](.*) http://www.myothersite.com/index.html [L,R=302]
    Netmar Web Services - Reliable Web Hosting for 15 years
    Shared hosting, business hosting, domain name registration, and VMware virtual servers
    Specializing in Drupal hosting
    Call 866-363-8627 (toll-free) and ask about our Drupal accounts

  5. #5
    Also, for that type of rewrite, you shouldn't need the rewrite conditions, only the rewrite rule. The rewrite rule itself is also it's own condition.

  6. #6
    Join Date
    Apr 2011
    Location
    Berkshire UK
    Posts
    9
    Thanks for the tips so far but they do not seem to work. Reading the mod_rewrite documentation again I get the impression that I have to use RewriteCond when I want to test on a query string. so Ive tried

    RewriteEngine on

    rewritecond %{REQUEST_URI} ^.*index\.php.*$
    RewriteCond %{QUERY_STRING} ^p=2[168]$
    RewriteRule ^.*$ http://www.myothersite.com/index.html [L,R=302]

    But that does not seem to work either.

    Is there any better ideas?

    Thanks

  7. #7
    Join Date
    Apr 2011
    Location
    Berkshire UK
    Posts
    9
    Thanks to g1smd in another forum I now have a working answer. For those who want to know how to do it the solution is:

    Code:
    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} (^|&)cPath=2[136](&|$)
    RewriteRule  ^([^/]+/)*(index\.php)?$ http://www.myothersite.com/? [L,R=302]
    This bit -
    Code:
    (^|&)cPath=2[136](&|$)
    - finds the query string in the calling url even if it is not the first and even if there are following parameters

    and this -
    Code:
    ^([^/]+/)*(index\.php)?$
    - makes sure that only the index.php page is redirected (when the condition above is met). The ? at the end prevents the "cPath=xx" from being appended to the othersite url

  8. #8

    Red face

    Quote Originally Posted by briggers View Post
    ... I want to test on a query string...
    Ah, that part i overlooked! Sorry about that. Yes, to match a query string, you MUST use a rewrite condition. Sorry again.

Similar Threads

  1. mod_Rewrite Problem
    By amaZe in forum Programming Discussion
    Replies: 3
    Last Post: 07-19-2010, 10:15 AM
  2. possible mod_rewrite problem
    By g1media.co.uk in forum Hosting Security and Technology
    Replies: 12
    Last Post: 03-01-2010, 03:00 PM
  3. mod_rewrite problem
    By demokrit in forum Programming Discussion
    Replies: 1
    Last Post: 12-07-2006, 11:24 AM
  4. mod_rewrite problem
    By shockuk in forum Programming Discussion
    Replies: 0
    Last Post: 05-21-2005, 08:16 AM
  5. Another mod_rewrite problem
    By pickle in forum Web Hosting
    Replies: 1
    Last Post: 07-27-2004, 03:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •