I want to be able to search in two sub-directories with .htaccess - apache

I want to be able to search in two sub-directories with .htaccess
I have the following directory structure
My problem is:
When for example a request on
resources/images/players_small_pigs/BMI10.png
is being received, I would like to be able to search in the path
webapp/public/resources/images/players_small_pigs/BMI10.png
is the file not to find here, I would like to continue the search in
webapp/adminpanel/resources/images/players_small_pigs/BMI10.png
Can some one help me to get it to work by using the .htaccess file? The problem throughout the entire folder structure resources, views, and webservices, I think it might be a bad structure I have and maybe I should put them all in a folder, rather than having it split up between a adminpanel and public folder, what would you recommend?
I have the following code in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(resources/.*)$ /webapp/public/$1 [L]
RewriteRule ^(resources/.*)$ /webapp/adminpanel/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/public/webservices/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/adminpanel/webservices/$1 [L]
RewriteRule ^(public/resources/images/)(sponsors/)?(.*)$ /webapp/$1$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} adminpanel
RewriteRule (.*) /webapp/adminpanel/views/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !adminpanel
RewriteRule (.*) /webapp/public/views/$1 [L]
RewriteRule ^$ /webapp/public/views/index.php [L]

You can give the following rule a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} [L]

Related

.HTACCESS Merging Two Rules

1.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ Ena.php?open=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^([^=]+)$
RewriteRule ^/?$ Ena.php?open=%1 [L,QSA]
2.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule "^(\x41|\x4F|\x53|\x4C\x4A|\x4E\x4A|D\xC5\xBD|\xC5\xA0|\xC4\‌​x90|\xC4\x8C|\xC4\x8‌​6|\xC5\xBD|\x00-\xF4‌​8FBFBF|[A-Z])[^/]*$" "/? open=Encyclopedia&letter=$1&term=$0" [NE,L,QSA]
I need these two Rules properly merged together. They work well alone (when one is deleted, and the other one left), but together they won't.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,END]
RewriteRule "^(\x41|\x4F|\x53|\x4[CE]\x4A|D\xC5\xBD|\xC5\xA0|\xC4(?:\‌​x90|\x8C|\x8‌​6)?|\xC5\xBD|\x00-\xF4‌​8FBFBF|[A-Z])[^/]*$" "/? open=Encyclopedia&letter=$1&term=$0" [NE,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ Ena.php?open=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^([^=]+)$
RewriteRule ^/?$ Ena.php?open=%1 [L,QSA]
What happens if you put them in the order above? First, we check if requested content exists on system, and serve it as is. The END directive is new to httpd-2.4+, and short-circuits all other directives.

Handle languages in .htaccess

I want to solve the next problem:
1) When entering the url without language, use English language as default
example:
http//localhost/plants/ or http//localhost/plants
or
http//localhost/plants/shop/accessories
2) When entering the url with language, pass that parameter as the language to use
example:
http//localhost/plants/es/shop/accessories
or
http//localhost/plants/es/ or http//localhost/plants/es
So far I have tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
If I comment the first RewriteRule and enter any url it works fine but always using the en language.
If I comment the second RewriteRule and I use a url with the language:
http//localhost/plants/es/
or
http//localhost/plants/es/shop/accessories
It works fine, but it doesn't set the default language to English when is not given.
Any idea why it doesn't work when I leave the two rules?
Thanks
Ps: I have removed the : after http
If plants is your document root, you should be able to use this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !/(en|es)
RewriteRule ^(.*)/?$ index.php?url=$1&lang=en [L,QSA]
RewriteRule ^(en|es)/?(.*)/?$ index.php?url=$2&lang=$1 [L,QSA]
Let me know how it works for you.
Found a solution doing this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
How can I improve this? It feels wrong I have to write twto time the same Cond
Put the following htaccess file inside the /plants directory:
RewriteEngine On
RewriteBase /plants
RewriteCond %{REQUEST_URI} !^/plants/e[ns](/|$) [NC]
RewriteCond %{QUERY_STRING} !lang=e[ns] [NC]
RewriteRule ^.*$ /plants/en/$0 [R=301,L]
RewriteRule ^(e[ns])/(.*)$ index.php?url=$2&lang=$1 [NC,L,QSA]
If the above still doesn't work, update your own approach to something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]

how to escape "/" from mod_rewrite

this is my .htaccess file thnx to #Gyrocode-com
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/(.*)$ fun.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/(.*)$ user.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L] #my problem is here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /jokes/$1/ [R]
everything works great except whene i try to go to this link
localhost/u/user1/2
which suppose to take me to page number 2 of user1
but instead it catches user1/2 as the value of variable (u)
and what I want is u=user1 ; p= 2;
Your rules can be refactored to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^p/([^/]+)/?$ fun.php?p=$1 [L]
RewriteRule ^u/([^/]+)/?$ user.php?u=$1 [L]
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L]
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteRule ^([^/]+)$ /jokes/$1/ [R,L]
Using ([^/]+) instead of (.*) to restrict matching to anything before /
Instead of using RewriteCond %{REQUEST_FILENAME} -f and RewriteCond %{REQUEST_FILENAME} -d before each rule it is better to use that in a separate rule as shown.

RewriteRule to redirect whole domain to get parameter

I want to redirect a whole url to a query parameter with a RewriteRule in .htaccess
for example: http://server.com/http://google.com should be redirected to
http://server.com/index.php?url=http://google.com
so far i'm just able to make this work: http://server.com/google.com but when a : or / is contained, it doesn't work..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=$1 [L,NC,QSA]
thanks for help!
RewriteRule patter strips multiple / into one, better use RewriteCond here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php?url=%1 [L,NC,QSA]
Why not just do the TLD and then add the http:// in the rule. This is how I would do it.
This is the way I would use it so it doesn't "look" invalid. http://server.com/google.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=http://$1 [L,NC,QSA]

RewriteCond of htaccess is not working properly

Below is my .htaccess file:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
I have some issues:
lib and applications are folders, but RewriteCond %{REQUEST_FILENAME} !-d has no effect, I have to write condition for lib and application separately.
The main issue is my api calls are not giving me result. I am calling my api as www.myweb.com/api/User/xyz%40gmail.com which is redirecting to www.myweb.com/api/index.php/api/User/xyz%40gmail.com, which is right. But calls to this are blank.
A RewriteCond only gets applied to the immediately following RewriteRule. Conditions aren't set globally nor get applied to more than one rule. You'll either need to repeat the condition for each rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
Or you can negate the condition and let it pass through first:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
This way, the opposite of your conditions get applied to a rule that simply says "do nothing, and no more rewriting". In order for any of the bottom 5 rules to get applied, the conditions at the top would not have been met.