RewriteCond of htaccess is not working properly - apache

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.

Related

htaccess rewriterule challenge

I'm building a MVC based CMS.
I'm trying to route all the urls as followed:
site.com/admin/... > admin.php?url=...
site.com/member/... > member.php?url=...
site.com/... > index.php?url=...
The first two are no problem
Options -MultiViews
RewriteEngine On
RewriteBase /cms/public_html/
RewriteCond %{REQuEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ admin.php?url=$1 [QSA,L]
RewriteRule ^member/(.*)$ member.php?url=$1 [QSA,L]
But when I want to add the third, that's where I get stuck. I've tried
RewriteRule (.+)$ index.php?url=$1 [QSA,L]
This however 'disables' the first two rules.
Then I tried:
RewriteRule !^admin(.+)$ index.php?url=$1 [QSA,L]
This works for the admin part, also the index is shown, however isn't the rest of the URL parsed in the ?url.
How can I get this to work?
I should also be able to add more site parts (such as site.com/gallery/).
How about another RewriteCond?
Note that you need to list the set of RewriteCond for each RewriteRule as they only apply once.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ admin.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^member/(.*)$ member.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/member(/|$)
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Demo for the last set here: http://htaccess.mwl.be?share=e7bf5c48-3332-56c1-a83b-d828b3c043a4

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

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]

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]

.htaccess RewriteRule country codes and urls

I am using the following .htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]
In order to achieve the following effect:
http://xyz.com/ca/test -> http://xyz.com/index.php?p=test&c=ca
http://xyz.com/fr/test -> http://xyz.com/index.php?p=test&c=fr
http://xyz.com/test -> http://xyz.com/index.php?p=test
But it is failing with a server error. Any ideas on how to fix it?
Thanks
RewriteCond conditions only apply to the RewriteRule that immediately follows the condition(s). Your last 2 rules don't have any conditions on them and the rules are looping. Just add the 2 conditions in front of the last 2 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]