mod_rewrite Question

I’m working on a website that requires me to allow the following URL structure:

http://www.example.com/{schoolname}/

i.e. http://www.example.com/newyorkprimary/

There maybe hundreds of different school names.

I want to have that rewritten as http://www.example.com/school.php?schoolid={schoolname}

i.e. http://www.example.com/school.php?schoolid=newyorkprimary

All of the examples I’ve seen seem to require an intermediate step, i.e. http://www.example.com/school/{schoolname}/

And they rewrite by doing something like:

RewriteEngine On
RewriteRule ^school/(.+)/$ /school.php?schoolid=$1 [NC]
RewriteRule ^school/(.+)$ /school.php?schoolid=$1 [NC]

I don’t want the /school/ directory in the URL, I want the school name to go straight after the root directory.

I can’t just rewrite all sub-directories because of course I do have other sub-directories that must remain unchanged, i.e. /images/, /js/, etc.

Any suggestions?

Thank you! :slight_smile:

Yeah I was wondering a bit about that. It’s not a highly used site so the traffic isn’t that great, but I’d still like to avoid taxing the server unduly.

I have some reasons for not wanting to use absolute links (I won’t complicate things by going into them here).

Now this is something that I might do just for this one page.

Thanks for the heads-up, much appreciated!

Out of curiousity, what rules did you use for that?
I know of a few ways to solve the relative files issue, but RewriteRules aren’t among them :slight_smile:

The set of RewriteRules that I came up with were:

# Because the school rewrite breaks relative URLs, we need to rewrite these
RewriteCond %{REQUEST_URI} .gif$ [OR]
RewriteCond %{REQUEST_URI} .png$ [OR]
RewriteCond %{REQUEST_URI} .jpe?g$
RewriteRule ^.*/images/(.+)$ /images/$1 [NC,L]
RewriteRule ^.*/js/(.+)$ /js/$1 [NC,L]
RewriteRule ^.*/all.css$ /all.css [NC,L]

# Handle URLs that contain school names
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /parent-order-1.php?schoolname=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /parent-order-1.php?schoolname=$1 [L]

This handles images in my images directory, JavaScript in my js directory, and the one CSS file that I’ve got.

These could probably be fine tuned a bit to make them more efficient but they seem to work ok in the testing I’ve done.

If you spot anything suspect then please let me know as I’m a real beginner with mod_rewrite!

Huh, pretty clever :slight_smile:

Though I’m afraid it’s a bit abusive of your server as there are easier ways that don’t force Apache to check so much.

  1. Instead of using relative links (like all.css) use absolute links in your HTML (i.e. /all.css)

  2. Use the HTML base tag, that’s what it’s there for

:slight_smile:

Thanks for the heads up, David.

Yes I did indeed run into problems with my relative links, but some more RewriteRule’s fixed those.

I would have preferred not to worry about the trailing / but for this website people will be told that they can go to a certain address to get their school’s info and I’m afraid that some will automatically type the / on the end of the URL because it looks like a directory name. So I wanted to make sure that they got to where they’re meant to be.

aweb4u,

Making the schoolname a directory will cause relative link problems - and makes the file test superfluous. Be sure that you really want to have those problems before using a trailing /.

Regards,

DK

Thanks very much for your help!

Using your suggestion I managed to get it working but I had to change it slightly:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /school.php?schoolid=$1 [L]

Note some extra /'s in there which I had to add before it worked.

Yes, you’d probably want to check if the requested URL is either an existing file or directory, and if it is, don’t execute the RewriteRule. That’s simple enough :slight_smile:


Options +SymLinks -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ school.php?schoolid=$1 [L]