homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Conditional mod rewrite?

 7:46 am on Apr 13, 2011 (gmt 0)

So I have a website that services many schools. (www.mysite.com) and I am using mod_rewrite as a way to keep the current school in the URL (for analytics reasons)

www.mysite.com/index.php?school=Pennstate would turn into www.mysite.com/school/Pennstate/index/

www.mysite.com/faq.php?school=Pennstate would turn into www.mysite.com/school/Pennstate/faq/

This is all fine and dandy (though a bit cumbersome and error prone)

My problem is, with the rewrite rule in place the main site when a school is NOT selected cant be accessed.

RewriteRule ^school/([^/\.]+)/([^/\.]+)/?$ /$2.php?school=$1

If anyone knows of a good way to make the rewrite rule work in both instances and/or make the rewrite less error prone please let me know, OR if you know of a better way to do this (while keeping the current school in the url) please let me know.

The only solution I have come across is using www.mysite.com/school/none/index/ as the home page and thats just ugly.

Thanks in advance



 7:43 pm on Apr 13, 2011 (gmt 0)

Are there other parts of the site using a parameter other than school as the main parameter?


 8:13 pm on Apr 13, 2011 (gmt 0)


I am new to mod_rewrite but you can give this a try.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)
RewriteRule ^/([^/\.]+) /school/%1/$1/index.php [QSA]

It worked on my test site. Like g1smd said, your site might be much more complex and require a better rewrite.



 8:21 pm on Apr 13, 2011 (gmt 0)

Scratch that, not enough testing, it is not going to work for other links outside index.php, faq.php breaks this.


 8:26 pm on Apr 13, 2011 (gmt 0)

That ruleset will need several changes:
- the optional & delimiters added before school and after the value,
- the [L] flag on the Rule.

The leading slash in the Rule pattern assumes usage in the server configuration file not in .htaccess.


 7:05 am on Apr 14, 2011 (gmt 0)

There are parameters other than school on some pages. For instance there is a search page that uses ?classid=1234,5678,7890 as well as some posts.

I guess the better question is if there is a better way to accomplish this.

I can grab data from the URL all day long with php but I havent figured out anyway to put things into the url without a form submission.
I'm on schoolselector.php and I select school1 redirecting me to schoolselector.php?school=school1 which is great, but when my user then click on faq it will take them to faq.php and the parameters are lost.

Thanks again


 7:15 am on Apr 14, 2011 (gmt 0)

The PHP script has to build the dynamic link in the href to show on the page for the user to click on.

URLs are defined in links.


 3:39 pm on Apr 14, 2011 (gmt 0)

> My problem is, with the rewrite rule in place the main site when a school is NOT selected can't be accessed.

What would the link "with no school selected" look like?
What filepath+querystring should a request for that link be rewritten to?

When doing this sort of thing, it is important (for yourself as well as for us here) to fully define both the linked/requested URL and the desired filepath for all possible cases (e.g. school selected vs. school not selected).

To further expand on g1smd's comment, a URL is created by your putting it on one of your Web pages. The server "connects" that URL, when requested by a browser or a search robots, to a file inside its filesystem. Keeping the two concepts of "URLs used out on the Web" and "filepaths used inside this server" separate and distinct is essential to successful implementation of rewrite rules and retention of sanity... :)



 6:20 am on Apr 15, 2011 (gmt 0)

www.mysite.com/index.php (or just www.mysite.com)
www.mysite.com/school/myschool/index.php (or just www.mysite.com/school/myschool/)
www.mysite.com/school/myschool/index/ (or just www.mysite.comschool/myschool/)
The first is how the url should look without the school selected, the next 2 are 2 ways to look at it when school is selected. (IE the main difference is /school/myschool/ is added when there is a school selected with 'myschool' being the name of a school)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved