Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a .htaccess file in the root directory of my project, that contains all the rewrite rules for the site to functions. Most of these rules belong to the mod section of the site, and only two of them belong to the front end, which the users use.

Problem is, when a front end user uses the site, apache ends up matching all the rules including the ones for the mods and signup etc, until it reaches the one for the end user, which I feel is unnecessary. I've checked this in the .htaccess log.

# Mod RewriteRules, some 12 in all 
RewriteRule ^/?(mod)/(all|new|edit|redo|reject)/(push)/?$ /moderator/index.php?mode=$2&push=0 [NC,L]
RewriteRule ^/?(mod)/(all|new|edit|redo|reject)/?$ /moderator/index.php?mode=$2 [NC,L]

#signUp - Again, not always needed
RewriteRule ^/?(signup)/?$ /acc/signup/index.php?a=signUp [NC,L]
RewriteRule ^/?(signup)/(process)/?$ /acc/signup/process/index.php [NC,L]

#signIn - Again, not always needed
RewriteRule ^/?(signin)/?$ /acc/signin/index.php?a=signIn [NC,L]
RewriteRule ^/?(signin)/(process)/?$ /acc/signin/process/index.php [NC,L]

#signOut - Again, not always needed
RewriteRule ^/?(signout)/?$ /acc/signout/index.php [NC,L]

# These are the only two that a front end user will use
# category/subCategory
RewriteRule ^/?([a-z-]+)/([a-z0-9-]+)/?$ /display/index.php?t=$1&s=$2 [NC,L]

# category
RewriteRule ^/?([a-z-]+)/?$ /display/index.php?t=$1 [NC,L]

If you look at the above rules, the last two are the only ones that a user using the front end will use. However, the cannot be placed on top, because they'll end up catching anything first, because of the way they are.

Can I place all rules for the moderator, signup and signin in their own directories, and do a rewrite rule in the root .htaccess file, that when it detects a url with mod, like /mod/all will send that request to the moderator directory. That way the root .htaccess file will have only the last two rules for the front end user along with others like no hot linking ones.

How can I do something like:

If (URL Starts with '/mod/' then send it to the '/moderator/' folder where the RewriteRules there will apply) or
If (URL Starts with '/signup/' then send it to the '/acc/' folder where the RewriteRules there will apply)
and so on.

My directory structure is as follows:

/acc/ ...handles account login and creation
/display/ ... handles front end display
/moderator/ ...the sites administrator

How can I achieve this?

share|improve this question
    
Are those rules causing you problems such as redirects? is it something aesthetic? are you having performance issues because of those non-matching rules? –  Binarysurf Jul 8 at 13:14
    
@Binarysurf There are several I've left out for this question. And why match each an every one when they aren't needed? That's why I decided on this. Any idea how to do the part I'm stuck with –  Norman Jul 8 at 13:33
    
Well, there could be many of them and don't cause any trouble at all, I've an .htaccess with 200 lines aprox, and as supposed just one RewriteRule will match on every request. But you have a valid reason, you don't want all of them together, let me post an answer and lets see if that will work for you. –  Binarysurf Jul 8 at 13:39
    
OK. Sounds good. I await your answer. –  Norman Jul 8 at 14:36
    
Norman, I've updated my post, let me know if it works for you. –  Binarysurf Jul 9 at 16:04

1 Answer 1

up vote 2 down vote accepted

Your root .htaccess should look like this:

# Mod RewriteRules, some 12 in all 
RewriteRule ^mod/(.*)$ /moderator/$1 [NC,L]

RewriteRule ^signup/(.*)$ /acc/signup/$1 [NC,L]

RewriteRule ^signin/(.*)$ /acc/signin/$1 [NC,L]

RewriteRule ^signout/(.*)$ /acc/signout/$1 [NC,L]

# These are the only two that a front end user will use
# category/subCategory
RewriteRule ^/?([a-z-]+)/([a-z0-9-]+)/?$ /display/index.php?t=$1&s=$2 [NC,L]

# category
RewriteRule ^/?([a-z-]+)/?$ /display/index.php?t=$1 [NC,L]

Moderator folder's .htaccess:

RewriteRule ^(all|new|edit|redo|reject)/push/?$ index.php?mode=$1&push=0 [NC,L]
RewriteRule ^(all|new|edit|redo|reject)/?$ index.php?mode=$1 [NC,L]

Signup folder's .htaccess:

RewriteRule ^$ index.php?a=signUp [NC,L]
RewriteRule ^process/?$ process/index.php [NC,L]

SignIn folder's .htaccess:

RewriteRule ^$ index.php?a=signIn [NC,L]
RewriteRule ^process/?$ process/index.php [NC,L]

SignOut folder's .htaccess:

RewriteRule ^$ index.php [NC,L]

Edit

These rules will match URL's like /signup or /signup/ or /signup/some.

RewriteCond %{REQUEST_URI} ^/mod$ [OR]
RewriteCond %{REQUEST_URI} ^/mod/.*
RewriteRule ^mod(.*)$ /moderator$1 [NC,L]

RewriteCond %{REQUEST_URI} ^/signup$ [OR]
RewriteCond %{REQUEST_URI} ^/signup/.*
RewriteRule ^signup(.*)$ /acc/signup$1 [NC,L]

RewriteCond %{REQUEST_URI} ^/signin$ [OR]
RewriteCond %{REQUEST_URI} ^/signin/.*    
RewriteRule ^signin(.*)$ /acc/signin$1 [NC,L]

RewriteCond %{REQUEST_URI} ^/signout$ [OR]
RewriteCond %{REQUEST_URI} ^/signout/.*    
RewriteRule ^signout(.*)$ /acc/signout$1 [NC,L]
share|improve this answer
    
Your answer works great. However, taking this rule as an example RewriteRule ^signup/(.*)$ /acc/signup/$1 [NC,L] - This only works if the URL supplied has a trailing / Eg; http://www.example.com/signin/ If no / is supplied at the end, it heads of to some other rule. I did try a number of things, but none will work. Can something be changed so it'll work without the ` and even with the ` –  Norman Jul 9 at 6:53
    
@Norman that's right, it only works with url's having trailing slash. Let me try something, and I'll update my answer as soon as I can. –  Binarysurf Jul 9 at 14:05
    
I did something like this for the trailing backslash problem, is this correct or will this fail somewhere RewriteRule ^mod/?(.*)/?$ /control/$1 [NC,L] –  Norman Jul 10 at 4:01
    
@Norman using RewriteRule ^mod/?(.*)/?$ /control/$1 [NC,L] will match URL's starting with mod followed by whatever, such as module, mode, moderator This htaccess tool is very helpful. –  Binarysurf Jul 10 at 4:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.