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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 /?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.