1

How do I maintain 2 separate .htaccess files so as to avoid dumping rules in the main .htaccess file.

For example: I want all the urls trailing after ^shows(/.*)?$ to point to .htaccess created under controllers/shows (.htaccess)

vzwick
  • 11,008
  • 5
  • 43
  • 63

1 Answers1

1

Depending on your rewrite rules, this can be accomplished with minimal rewriting (heh) of the .htaccess file. Take for example this .htaccess file stored in the webroot /var/www/html:

RewriteEngine On

RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1
Rewriterule ^blog/(.*)? blog/index.php?id=$1

We're doing two kinds of rewrite here:

  • www.example.com/foo/help/123 -> wwww.example.com/foo/help.php?q=123
  • www.example.com/blog/myarticle -> www.example.com/blog/index.php?id=myarticle

Notice the first RewriteRule is using absolute paths, while the second is using a relative path.

If I were to move the first RewriteRule into /var/www/html/foo/.htaccess, the file would look like this:

RewriteEngine On

RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1

Note that nothing needs to change, since you're using an absolute path.

Now if I were to move the second RewriteRule into /var/www/html/blog/.htaccess, the file would look like this:

RewriteEngine On

RewriteBase /blog/
RewriteRule ^(.*)? index.php?id=$1

Note the paths in the RewriteRule were stripped to make them relative to the current directory, which is now /blog. Also note the RewriteBase directive, which is necessary when using relative rewrite rules in a subdirectory.

Check out this question for another example of using subdirectory .htaccess rewrite rules.

AfroThundr
  • 1,175
  • 2
  • 17
  • 28