Solved

conditions on RewriteRule

Posted on 2012-04-03
12
256 Views
Last Modified: 2012-04-03
Hello,

I have a mod rewrite which works great, it is grabbing all page addresses in a certain folder and rewriting them into PHP usable query string.

RewriteRule car-auto-insurance-news/([a-z0-9-_]+)\.php$ car-auto-insurance-news/news_page.php?page=$1  [nc]

the problem is that is grabs every page in that folder.. i want to add a condition which is..

if page = insurance-news-articles.php, then DONT run the re-write, but do it to every other page


so roughly I need it to look like this in my .htacess file, i just dont know how to write it

if (page == insurance-news-articles.php)  {

 // do nothing

} else {

 RewriteRule car-auto-insurance-news/([a-z0-9-_]+)\.php$ car-auto-insurance-news/news_page.php?page=$1  [nc]

}
0
Comment
Question by:jblayney
  • 6
  • 5
12 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 37801547
Add the following immediately above that RewriteRule

RewriteCond %{REQUEST_URI} !^/insurance-news-articles.php$ [NC]

Open in new window


Based on your existing RewriteRule you may or may not need that carrot and slash before insurance in the RewriteCond, depends on what the base URL is for these rules.  Not sure since there isn't much context given.

The main parts here are request_uri and the !
0
 
LVL 1

Author Comment

by:jblayney
ID: 37801676
Hello Papertip,
that works, it has a weird bug though, I have a  PHP recordset on insurance-news-articles.php and it is not running anymore, if I remove the htacess code the page runs fine..
the rewrite conditions seem to be killing my database connection and display, any ideas?
0
 
LVL 1

Author Comment

by:jblayney
ID: 37801717
Hi Papertip,

on testing further i just realized it isnt working at all, it is still redirecting the page to news_page.php it just rewrote the URL
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37801725
Just a thought... It might be easier to keep things organized if your scripts that need rewriting are in a separate directory from those that do not need rewriting.
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 37801742
Try adding another RewriteCond

RewriteCond %{REQUEST_URI} car-auto-insurance-news/([a-z0-9-_]+)\.php$

Open in new window

0
 
LVL 21

Expert Comment

by:Papertrip
ID: 37801751
If Ray has a php-based suggestion for this like he mentioned I would listen to his advice.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Expert Comment

by:Papertrip
ID: 37801786
Also, for now and future reference, posting more context is always helpful.  All you gave was a single RewriteRule and we have no idea what else is happening in your conf files that could or could not be affecting this.
0
 
LVL 1

Author Comment

by:jblayney
ID: 37801788
Hi Papertip and Ray,

Ray I had that thought from the beginning, client was very specific about URL for SEO, I think i may get them to do your suggestion as it gives 1 more level of keyword/ folder writing

Papertip, no luck, i tried with both, each separately, with slash, without, right now it looks like so.


RewriteCond %{REQUEST_URI} !^/insurance-news-articles.php$ [NC]
RewriteCond %{REQUEST_URI} car-auto-insurance-news/([a-z0-9-_]+)\.php$
RewriteRule car-auto-insurance-news/([a-z0-9-_]+)\.php$ car-auto-insurance-news/news_page.php?page=$1  [nc]
0
 
LVL 1

Author Comment

by:jblayney
ID: 37801802
Papertip, the whole file


RewriteEngine On
Options +FollowSymLinks

#non www to www
RewriteCond %{HTTP_HOST} ^website\.ca
RewriteRule (.*) http://www.website.ca/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/insurance-news-articles.php$ [NC]
RewriteCond %{REQUEST_URI} car-auto-insurance-news/([a-z0-9-_]+)\.php$
RewriteRule car-auto-insurance-news/([a-z0-9-_]+)\.php$ car-auto-insurance-news/news_page.php?page=$1  [nc]


# Added for Custom Errors
ErrorDocument 401 http://www.website.ca/400.php
ErrorDocument 401 http://www.website.ca/401.php
ErrorDocument 403 http://www.website.ca/403.php
ErrorDocument 404 http://www.website.ca/404.php
ErrorDocument 500 http://www.website.ca/500.php

redirect 301 /index.html http://www.website.ca/index.php
redirect 301 /web/index.php http://www.website.ca/index.php
redirect 301 /web/services.php http://www.website.ca/index.php
redirect 301 /web/auto-insurance.php http://www.website.ca/index.php
redirect 301 /web/privacy.php http://www.website.ca/index.php
redirect 301 /web/auto-insurance.php http://www.website.ca/index.php
redirect 301 /web/auto-insurance.php http://www.website.ca/car-auto-insurance/car-insurance-ontario/insurance-quotes-ontario.php
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 37801812
I'll step back and let Ray take a stab from his side since it sounds like a better idea.  Sorry for not testing those rewritecond's first, I will have some time soon to test unless you get it resolved in the meantime.
0
 
LVL 1

Author Comment

by:jblayney
ID: 37801901
Hi Papertip, the recordset problem is not the problem, the condition doesn't work...

ray doesn't have a PHP solution, a good solution, but one I have to sell
0
 
LVL 1

Author Closing Comment

by:jblayney
ID: 37801962
client loved the idea, easy sell, i shouldn't have wasted 2 hours messing with htacess
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now