Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

RewriteRule subdirectory creation

I have a number of departments I need to categorize using mod_rewrite. Several departments have full websites with their own subdirectories of information, while others have only a template of information that is generated from a dynamic PHP page. I would like these templates, when served however, to look like they are in their own subdirectory. For example:

/department/smith - this directory exists, so the link going to the Smith department connects automatically to this directory

/department/jones - this directory does NOT exist, so it is created using mod.rewrite and department.php, which is sitting in the /department/ folder.

My .htaccess file is set as follows:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteBase   /department/
      RewriteRule ^([^.]+)\$ department.php?url_only=$1
</IfModule>

url_only is the database field that forces the recordset's clean URL entry.

Can this be achieved? If so, how?

Thanks.
0
matthutaff
Asked:
matthutaff
  • 3
  • 2
1 Solution
 
jentulmanCommented:
Which bit of it are you having the problem with?
The basic theory of what you are trying to acheive is sound.
0
 
matthutaffAuthor Commented:
Just the execution/writing of the RewriteRule, really.
0
 
jentulmanCommented:
.htaccess in the department folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /department/
RewriteRule ^department/([^/]*)/? department.php?url_only=$1

it would let you access existing files and folders but a call to something that doesnt exist like

/department/jones
/department/jones/
/department/jones/wholeheapojunk

will redirect to

/department/department.php?url_only=jones

the rewrite rule
RewriteRule ^department/([^/]*)/? department.php?url_only=$1
says is looking for any non '/' characters ( [^/]* ) that occur between 'department/' and and optional '/'
0
 
matthutaffAuthor Commented:
Hi,

I've tried the solution and it's not registering the department.php file. A 404 error appears when you try to visit the jones department. Does the parh for department.php need to be identified?
0
 
jentulmanCommented:
I'm really sorry I forgot to change something after my testing.

you either need to

1. move the .htaccess file to the parent folder of the department folder

2. leave it in the department folder and change the rule to

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^/]*)/? department.php?url_only=$1

Sorry again.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now