Jump to content

[SOLVED] Rewrite rule ignoring directories


jcombs_31

Recommended Posts

I have a basic directory structure that I want to use with a rewrite rule.  Here is the basic directory structure

 

/
  /info
     file1.php
     file2.php
  /schedules
     file1.php
  /staff
     file2.php
  info.php
  schedules.php
  staff.php

 

So you see I have php files with the same names as the directories.

 

In info.php for example, I will have a switch to include a file inside the info directory.  Something like

 

$page = $_GET['page'];

switch($page) {
  case 'file1'
     include('info/file1.php);
  break;

  ...
}

 

I want my URL to look like this [mysite.com/info/file1]

 

So, I thought I could write some pretty basic rewrite rule  ( mind you I don't really know much of anything about mod_rewrite )

 

What I have is this

 

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}
RewriteCond %{REQUEST_FILENAME}

RewriteRule ^info/([a-z]+)/?$ info.php?page=$1 [NC,L]

 

But if I go to the url [mysite.com/info/file1] , I get file1, but not included as part of info.php

 

If I simply change the rule to

 

RewriteRule ^somethingelse/([a-z]+)/?$ info.php?page=$1 [NC,L]

 

and then visit [mysite.com/somethingelse/file1] it works fine.  So, I'm having a problem with the rewrite matching the directory name. 

 

I could just be doing this all wrong, so if anyone can give a good example and explanation of how to accomplish what i want, that would be great :)

mysite.com/info/file1

 

 

It's strange that that works.

 

 

assuming "file1" is really a valid value, the problem could be that ([a-z]+) does not match numbers.  Guessing that's just a random example though.

 

 

Aside from that, I doubt this will work, but it might be worth a try:

 

RewriteRule ^info/([a-z]+)/?$ /info.php?page=$1 [NC,L]

Yes Corbin, that was a random example, the urls don't actually have numbers.  It seems I found the problem.  I had to disable multiviews for the rewriting to work correctly.  I couldn't figure this out, because my rules worked in a production server, but I just recently set up my testing server and it was giving me problems.

 

This was a good few hours of aggravation. 

You should mark this as solved.

 

I understand that your discovery is that multiviews supercedes mod_rewrite and doesn't even attempt to rewrite the solution it finds. That's good to know.

 

As an aside, would you script be improved by adding a / to the beginning of your regex so?

 


RewriteRule ^/info/([a-z]+)/?$ info.php?page=$1 [NC,L]

 

Or doesn't that work at all? Shouldn''t the request be starting with /?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.