Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Conditional mod rewrite?

     

bissquitt

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

10+ Year Member



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
Michael

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

zimurgy

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



bissquitt,

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.

HTH
Z

zimurgy

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.

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

bissquitt

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

10+ Year Member



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.e.
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

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> 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... :)

Jim

bissquitt

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

10+ Year Member



www.mysite.com/index.php (or just www.mysite.com)
www.mysite.com/faq.php
www.mysite.com/contact.php
--------------------------------
www.mysite.com/school/myschool/index.php (or just www.mysite.com/school/myschool/)
www.mysite.com/school/myschool/faq.php
www.mysite.com/school/myschool/contact.php
--------------------------------
www.mysite.com/school/myschool/index/ (or just www.mysite.comschool/myschool/)
www.mysite.com/school/myschool/faq/
www.mysite.com/school/myschool/contact/
----------------------------------
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)

Michael
 

Featured Threads

Hot Threads This Week

Hot Threads This Month