Mobile app version of vmapp.org
Login or Join
Shanna517

: RewriteRule to send all URLs with `mod` in them to the `moderator` directory I have a .htaccess file in the root directory of my project, that contains all the rewrite rules for the site to

@Shanna517

Posted in: #Apache #Htaccess #ModRewrite #UrlRewriting

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=&push=0 [NC,L]
RewriteRule ^/?(mod)/(all|new|edit|redo|reject)/?$ /moderator/index.php?mode= [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=&s= [NC,L]

# category
RewriteRule ^/?([a-z-]+)/?$ /display/index.php?t= [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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

1 Comments

Sorted by latest first Latest Oldest Best

 

@Candy875

Your root .htaccess should look like this:

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

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

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

RewriteRule ^signout/(.*)$ /acc/signout/ [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=&s= [NC,L]

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


Moderator folder's .htaccess:

RewriteRule ^(all|new|edit|redo|reject)/push/?$ index.php?mode=&push=0 [NC,L]
RewriteRule ^(all|new|edit|redo|reject)/?$ index.php?mode= [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 [NC,L]

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

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

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme