send all /mod/users URLs to a folder called moderator - apache

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.
How 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.
Something like:
If (URL Starts with '/mod/')
then send it to the '/moderator/' folder where the RewriteRules there will apply)
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
part of my .htaccess file
# 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.

Have root .htaccess like this:
RewriteEngine On
# forward mod|signup|signin|signout to /moderator/
RewriteRule ^(mod|signup|signin|signout)(/.*)?$ /moderator%{REQUEST_URI} [L,NC]
# 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,QSA]
# category
RewriteRule ^/?([a-z-]+)/?$ /display/index.php?t=$1 [NC,L,QSA]
Then have /moderator/.htaccess like this:
RewriteEngine On
RewriteBase /moderator/
RewriteRule ^/?(mod)/(all|new|edit|redo|reject)/(push)/?$ index.php?mode=$2&push=0 [NC,L,QSA]
RewriteRule ^/?(mod)/(all|new|edit|redo|reject)/?$ index.php?mode=$2 [NC,L,QSA]
#signUp - Again, not always needed
RewriteRule ^/?(signup)/?$ /acc/signup/index.php?a=signUp [NC,L,QSA]
RewriteRule ^/?(signup)/(process)/?$ /acc/signup/process/index.php [NC,L,QSA]
#signIn - Again, not always needed
RewriteRule ^/?(signin)/?$ /acc/signin/index.php?a=signIn [NC,L,QSA]
RewriteRule ^/?(signin)/(process)/?$ /acc/signin/process/index.php [NC,L,QSA]
#signOut - Again, not always needed
RewriteRule ^/?(signout)/?$ /acc/signout/index.php [NC,L,QSA]

Related

Dynamically hide all sub folder but initial

Essentially, I need to edit the .htaccess file to develop a way to hide folder structure after /file. For example, if I have this URL:
https://www.example.com/file/page/work/assignments.php?
I should only see https://www.example.com/file/assignments.php?
How can this be accomplished?
So far I have tried the following, but it does hide all folders after first:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
You can do something like the following to "hide" page/work after the /file/ directory and before the last path segment.
This needs to go in the root .htaccess file.
RewriteEngine On
# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]
# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]
The will externally redirect a request for /folder/page/work/assignments.php?something to /folder/assignments.php?something. And internally rewrite the request back again.
NB: You should already be linking to /folder/<file> internally. The initial redirect to remove page/work is just for SEO if you are changing an existing URL structure. Otherwise, this is not strictly necessary.
A look at your existing rule...
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
This would seem to be unrelated to what you are trying to achieve. It would internally rewrite a request for /<anything> to /subfolder/<anything>, essentially "hiding" /subfolder.
There is no need to check the requested Host header unless you are accepting requests to multiple domains and this is specific to just one of those domains. (You've made no mention of this in your question.)

Images don't load after htaccess redirect

I wanted to redirect all IPs to a specific page except my IP. and I successfully did that, but if that page has some pictures they won't ever load. Tried more than one solution, but nothing works.
How I redirect them using .htacess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (?:^|/)(css|js|img)/(.+)$ /$1/$2 [NC,QSA,L]
RewriteCond %{HTTP:X-FORWARDED-FOR} !^22\.22\.22\.22
RewriteCond %{REQUEST_URI} !/test.php$ [NC]
RewriteRule .* /test.php [R=302,L]
Images (and CSS and Javascript) don't load, because the first rule causes a rewrite loop. If you just want to serve requests to subfolders css, js or img without modification, you can exit the rule chain by using - as the target, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
RewriteRule ^(?:css|js|img)/ - [NC,L]

htaccess - Redirect to subfolder without changing browser URL

I've a domain that contains a subfolder with the web app structure. I added a .htaccess on my root domain to point the public folder on my subfolder web app. It works fine, but when I type www.example.com the browser URL changes to www.example.com/subfolder/public, but I would like that it doesn't change.
This is my .htaccess:
RewriteEngine On
RewriteRule ^.*$ subfolder/public [NC,L]
EDIT
This first .htaccess is used to redirect on subfolder/public, where there is an other .htaccess that makes all the works.
Here the code of the second .htaccess located on www.example.com/subfolder/public/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Sorry, just realised what is happening. It has nothing to do with the second .htaccess file in the subdirectory, as mentioned in comments.
RewriteRule ^.*$ subfolder/public [NC,L]
Since public is a physical directory on the file system, you need to include a trailing slash when internally rewriting to that directory. Otherwise, mod_dir is going to try to "fix" the URL by appending a slash - that is where the external redirect is coming from. (mod_dir implicitly triggers an external redirect from subfolder/public to subfolder/public/.)
So, try the following instead in your root .htaccess file:
RewriteRule .* subfolder/public/ [L]
The important thing is the trailing slash. The anchors (^ and $) on the RewriteRule pattern are not required, since you are matching everything. And the NC flag is also not required for the same reason.
As always, make sure the browser cache is clear before testing.
UPDATE#1: The single directive above rewrites everything, including static resources, to the directory subfolder/public/ which then relies on the second .htaccess file in the subdirectory to correctly route the request. In order to allow static resources to be rewritten correctly (represented in the HTML as root-relative URL-paths, of the form "/js/myjs.js") then you will need additional directives in order to rewrite these.
For example, to specifically rewrite all .js and .css files to the real location in /subfolder/public/...
# Rewrite static resources
RewriteRule (.+\.(?:js|css))$ subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
UPDATE#2: To make the above more general, and to rewrite any static resource (images, PDFs, .txt, etc...) we can check for the existence of the file before rewriting, something like:
# Rewrite static resources
RewriteCond %{DOCUMENT_ROOT}/subfolder/public/$1 -f
RewriteRule (.+) subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
This will mean that if any .css does not exist it will be passed through to subfolder/public/.

ReWrite Rules Issue

I seem to be having an issue with my Apache Rewrites
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/?$ / [NC,L,R=301]
RewriteRule ^/$ wordpress/ [NC,L]
I simply need to remove /wordpress from the URL as I have pages within Wordpress I want to be seen as the main directory
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
Any help?
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/(.*)$ blog/$1 [L]
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
So it looks like you want to redirect the browser if someone makes a request for domain.com/wordpress/ to a URL without the wordpress bit, then internally rewrite the wordpress bit back into the URI? That's definitely do-able but if you have wordpress rewrite rules somewhere they're not going to play nicely with each other at all.
Any rules in the /wordpress directory will supercede any rules you put in the document root, which is where these rules need to go, and your remove-the-wordress-from-URI rules will be completely ignored. Even if you have rule inheritance turned on, the rules in the /wordpress directory will get executed first.
If all of your wordpress rules are actually in the document root's htaccess file, then just make sure to put these before the wordpress ones:
RewriteEngine on
RewriteBase /
# redirect the browser if someone makes a request for domain.com/wordpress/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /wordpress/
RewriteRule ^/?wordpress/(.*)$ /$1 [L,R=301]
# internally rewrite the wordpress bit back into the URI
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -f [OR]
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -d
RewriteRule ^(.*)$ /wordpress/$1 [L]

How can I make my .htaccess skip rules conditionally?

I want to redirect all URLs that do not contain "_js","_css", "_img", etc., to my dispatch script. Somehow it doesn't work.
For instance, all files inside my /_js/ folder are unaccessible (meaning: they are sent to index.php instead of getting to the physical files residing in that folder).
Here is my htaccess:
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^/(_admin/¦_css/¦_js/¦_img/)
RewriteRule . - [S=9]
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://%{HTTP_HOST}/$1/?%{QUERY_STRING} [NC,R,L]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&%{QUERY_STRING} [NC,L]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1&%{QUERY_STRING} [NC,L]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&%{QUERY_STRING} [NC,L]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9_\-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]
# ONE PARAM
RewriteRule ^([a-z0-9_\-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L]
Edit:
Note my folder structure. Could it be the problem source?
I have a "v1" and "v2" folder structure. This .htaccess sits in the "v2" folder. one level above, i have a .htaccess that redirects all requests to "v2".
root
L.htaccess << dispatch between v1 and v2 folders
L v1 L v2 L.htaccess << the .htaccess code posted above
L _admin L all my website files & folders
You’re using the wrong character, ¦ (broken bar, U+00A6) instead of | (vertical line, U+007C), and wrong pattern for REQUEST_URI.
RewriteCond %{REQUEST_URI} ^/v2/(_admin/|_css/|_js/|_img/)
RewriteRule . - [S=9]
Or for the .htaccess file in your v2 directory just:
RewriteRule ^_(admin|css|js|img)/ - [S=9]
Here are some random fixes that may or may not fix the actual problem, which is not totally clear (see my comment) and which could be helped if you control the server and enable the RewriteLog, via:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
but you have to put this in main server configuration.
That said, two main problems:
Lack of use of QSA flag (not really relevant)
Excessive use of slashes (probably relevant in the actual problem, see new version of the skipping rule)
Here goes the modified file
IndexIgnore *
Options +FollowSymLinks
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_URI} ^(_admin¦_css¦_js¦_img)
RewriteRule . - [L]
# Externally redirect to add missing trailing slash. Not really needed, AFAICS
# RewriteRule ^(([^/]+/)*[^/]+)$ http://%{HTTP_HOST}/$1/ [NC,R,L,QSA]
# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]
# FIVE PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]
# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3&content=$4 [NC,L,QSA]
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2&menu=$3 [NC,L,QSA]
# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/?$ index.php?section=downloads&item=$1 [NC,L,QSA]
# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?section=$1&item=$2 [NC,L,QSA]
# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([^/]+)/?$ index.php?tag=$1 [NC,L,QSA]
# ONE PARAM
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]
EDIT: Given the folder structure explained, try adding to the v2 .htaccess at the beginning the following:
RewriteBase /
You still haven't explained if you can or cannot use RewriteLog (I presume you can't)