hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Another mod_rewrite problem
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Another mod_rewrite problem

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 06-09-2011, 05:05 AM
briggers briggers is offline
Newbie
 
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

Reply With Quote


Sponsored Links
  #2  
Old 06-09-2011, 05:10 AM
dtang4 dtang4 is offline
Newbie
 
Join Date: Jun 2011
Posts: 10
Why do you have the [168]?

Reply With Quote
  #3  
Old 06-09-2011, 05:30 AM
briggers briggers is offline
Newbie
 
Join Date: Apr 2011
Location: Berkshire UK
Posts: 9
'Cause that matches 21, 26, or 28 and no other number ( I think)

Reply With Quote
Sponsored Links
  #4  
Old 06-11-2011, 09:22 AM
netmar netmar is offline
Junior Guru
 
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

Reply With Quote
  #5  
Old 06-13-2011, 02:48 AM
ionisis ionisis is offline
Newbie
 
Join Date: Jun 2011
Posts: 18
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.

Reply With Quote
  #6  
Old 06-13-2011, 09:39 AM
briggers briggers is offline
Newbie
 
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

Reply With Quote
  #7  
Old 06-13-2011, 11:26 AM
briggers briggers is offline
Newbie
 
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

Reply With Quote
  #8  
Old 06-13-2011, 12:01 PM
ionisis ionisis is offline
Newbie
 
Join Date: Jun 2011
Posts: 18
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.

Reply With Quote
Reply

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

Related posts from TheWhir.com
Title Type Date Posted
Outbound Spam Causing Sleepless Nights? Blog 2013-05-13 09:52:21
How To Deliver Products Customers Will Pay For with David Koston - HostingCon 2012 Web Hosting News 2012-07-18 10:46:16
Web Host WebHostForASP.net Launches VPS Hosting Service Web Hosting News 2012-06-29 14:52:22
eleven Survey Lists Spam as Greatest Email Security Threat in Ten Years Web Hosting News 2011-12-02 21:50:09
The Lesson of the Ten, with Jeff Hardy Web Hosting News 2011-08-08 22:12:01


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?