Jump to content
Larry Ullman's Book Forums

Help! One Rewriterule Not Working


Recommended Posts

I have the following in my .htaccess file:

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^home$ index.php [L]
RewriteRule ^bf/([A-Za-z0-9-]+)$ /bf/index.php?content=$1
RewriteRule ^bf/([A_Za-z0-9]+)/([0-9-]+)$ /bf/index.php?content=$1&event_id=$2
RewriteRule ^bf/([A_Za-z0-9]+)/([0-9-]+)$ /bf/index.php?content=$1&article_id=$2

Note the rule (second from bottom) for Events, this works fine, but the bottom rule does not. What I mean is that the page does not receive the article_id variable. When testing the $_GET variable article_id, I get nothing. *However* if I should reverse the order of the bottom two rules then the News page receives the article_id variable, but the Events page no longer receives the event_id variable. I understood that Apache goes down the list of RewriteRule(s) and applies the rule when it finds a match, but continues going down the list to the end if there is no match. Why does this work for one but not both. Why does it not pass by the Event rule to get to the News rule which contains the correct variable placeholder?

 

Example links for the above rules are as follows:

http://mydomain.com/bf/events/27
or
http://mydomain.com/bf/news/3

 

Any ideas why this is not working?

Link to comment
Share on other sites

After looking at some info I had on RewriteRules, it appears that the only thing matched is the pattern, differences in the substitution variables are not considered. Because the pattern is the same for both Events and News, Apache is passing along event_id (when it is first) instead of article_id (when it is second). So then how can I differentiate between the two rules?

 

Based on Larry's modular website concept, I have the site home page (index.php) in the root directory, and I have a content directory "bf". Within the bf directory there is another index.php file which handles all of the dynamic pages. I pass along the content variable in each link so that the correct page is served; my second RewriteRule takes care of that. Now I want to pass along a record_id so I can show the requested record on the requested page. How can I do that when the pattern is the same for both News and Events, when only the record_id variable is different?

 

I hope this makes sense what I'm saying.

Link to comment
Share on other sites

RewriteRule ^bf/([A_Za-z0-9]+)/([0-9-]+)$ /bf/index.php?content=$1&event_id=$2
RewriteRule ^bf/([A_Za-z0-9]+)/([0-9-]+)$ /bf/index.php?content=$1&article_id=$2

Basically as you have said... Rewrite rule matches the first instance... in this example...

www.yourdomain.com/bf/events/26 and www.yourdomain.com/bf/news/26 would both match "RewriteRule ^bf/([A_Za-z0-9]+)/([0-9-]+)$ /bf/index.php?content=$1&event_id=$2"

Instead of trying to set 2 ReWriteRule's on this I would think the best way would be in the index.php itself, as there isn't really a way without changing the structure of the rules to get this to work.

Make the event_id and article_id as just id.

Take the last rule off and simply in the index.php make a if clause, for example:

$event_id = "";
$article_id = "";
if ($_GET['content'] == "events") {
$event_id = $_GET['id'];
} elseif ($_GET['content'] == "news") {
$article_id = $_GET['id'];
} else {
$error = "Some sort of error here";
}

 

Do the obvious checks of whether content and a id exist and whether they are valid etc. Also don't forget to set $event_id and $article_id above so then no error's appear.

 

Hope that helps

  • Upvote 1
Link to comment
Share on other sites

Craig,

 

Thanks for the suggestion and for replying. I found a different solution which is easier for me, but your idea might actually help me with another issue I'm facing.

 

Anyway here are the RewriteRule changes:

 

RewriteRule ^bf/events/([0-9-]+)$ /bf/index.php?content=events&event_id=$1

RewriteRule ^bf/news/([0-9-]+)$ /bf/index.php?content=news&article_id=$1

 

Thanks again for your help.

 

Cheers.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...