?
Solved

Multiple htaccess rewrite rules

Posted on 2007-10-18
9
Medium Priority
?
2,192 Views
Last Modified: 2013-12-13
I currently have a web site that uses the following rule:

<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteBase   /feature/travel/
      RewriteRule ^([^.]+)\.php$ country.php?country=$1
</IfModule>

We are planning to add two other sections to the travel directory, one a blog of someone traveling, and another of people who have traveled describing why they did. How do I have multiple rewrite rules like the one above? Something like this:

RewriteRule ^([^.]+)\.php$ country.php?country=$1
RewriteRule ^([^.]+)\.php$ blog.php?entry=$1
RewriteRule ^([^.]+)\.php$ profile.php?profile=$1

Your help is much appreciated!
0
Comment
Question by:matthutaff
  • 5
  • 4
9 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 20104218
You can try
RewriteRule ^([^.]+/blog/[^.]+)\.php$ blog.php?entry=$1 [L]
RewriteRule ^([^.]+/profile/[^.]+)\.php$ profile.php?entry=$1 [L]
// catch rest
RewriteRule ^([^.]+)\.php$ country.php?country=$1

So the part contains a /blog/ or /profile/ those pages will be rewritten.
0
 

Author Comment

by:matthutaff
ID: 20104429
That could work well with the blog, but the profile information would be in the same directory as the country information. Is that possible?
0
 

Author Comment

by:matthutaff
ID: 20125226
I tried your rewrite rule and it breaks my other rewrite.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:hernst42
ID: 20125273
Can you provide examples for all those types of links and to which script they should be rewritten? like
/xyz/blog/test => /blog.php?entry=$1
The more the better. So we might be able to create a matching rewrite rule. Are these the only rewrite rules active?
0
 

Author Comment

by:matthutaff
ID: 20125324
Sure, here's the current rewrite rule and the result:

RewriteBase   /academics/off-campus/
RewriteRule ^([^.]+)\.php$ program.php?country=$1 [L]

This turns:

/academics/off-campus/program.php?country=canada

into:

/academics/off-campus/canada.php

I'd like to add another one (or two) that would turn:

/academics/off-campus/profile.php?url_only=firstname_lastname

into:

/academics/off-campus/profiles/firstname-lastname.php

Thanks!
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20125410

To rewrite /academics/off-campus/profiles/firstname-lastname.php
to /academics/off-campus/profile.php?url_only=firstname_lastname
Try
RewriteRule ^profiles/([^-]+)-([^.]+)\.php$ profile.php?url_only=$1_$2 [L]

This should not screw off the already existing Rule
0
 

Author Comment

by:matthutaff
ID: 20125501
I adapted it to this because firstname_lastname is one field:

RewriteRule ^profiles/([^.]+)\.php$ profile.php?url_only=$1 [L]

But it's still using program.php as the base of information.

I've put this rule beneath the one for the programs.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 20125656
So I tried this and it seems to work (in case you don't have something like canada/toronto.php which should also be rewritten:

RewriteEngine On
RewriteBase /~hernst/ee/22902359/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^./]+)\.php$ program.php?country=$1 [L]
RewriteRule ^profiles/([^.]+)\.php$ profile.php?url_only=$1 [L]
0
 

Author Comment

by:matthutaff
ID: 20125936
That did the trick. Any chance there's a way to make an addendum to the rule that sends visitors to our standard error page if they burp up a non-existent name?
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 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