htaccess multiple RewriteConds - apache

This is my site root
/public_html
beta/
.htaccess
public/
index.php
.htaccess
The first .htaccess inside beta folder is for changing dir from mysite.com/beta to mysite.com/beta/public:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com/$
RewriteCond %{REQUEST_URI} !beta/public/
RewriteRule (.*) /beta/public/$1 [L]
The other.htaccess inside public folder is for shorten url from index.php?photo_id=id to /id:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?photo_id=$1
How can I make these work both since for now only one of them work not together? Thanks

Why do you have "non public" stuff in public_html? If you want it private, shouldn't you place it in a private directory?

For the beta htaccess file, try removing the absolute URLs
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\)?mysite\.com$ [NC]
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [L]
Note that the trailing slash needs to be removed from the HTTP_HOST, there's not slashes in that variable.
For the public folder, also remove the absolute URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?photo_id=$1

Related

.htaccess check if rewrite subdirectory exists

I want to point each subdomain to a corresponding directory by default. To make things simple subdomain and directoryname are the same. This works well but is there any way to check if the actual directory exists? If not I don't want to do the rewrite.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^(/)?$ %1/index.php [L]
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{DOCUMENT_ROOT}/%1/index.php -f
RewriteRule ^ %1/index.php [L]
This will rewrite to a subdomain.example.com/foobar to subdomain.example.com/subdomain/index.php only if subdomain directory exists with a index.php file in it.

url rewrite for subfolder

I'm trying to do rewrite only for a subdirectory, but I have a problem with RewriteCond.
All my files are in /abc.
Here is my htaccess (/abc/.htaccess) file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/static/ [NC,OR]
RewriteCond %{REQUEST_URI} ^/files/ [NC]
RewriteRule . - [L]
RewriteRule (.*) index.php?_url=$1 [QSA,L]
So I want to rewrite every url to index.php except the existing files in /static and /files. But this code is rewriting /static/a.jpg too, since the request was /abc/static/a.jpg. I tried setting RewriteBase to /abc/, but nothing changed.
Any idea how can I solve this?
Have your .htaccess like this in /abc/.htaccess:
RewriteEngine on
RewriteBase /abc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(static|files)(/|$) [NC]
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?_url=$1 [QSA,L]

(.htaccess) how to mod_rewrite a tilde user site to subfolder within same site

I have been working this issue for several nights with no luck. I'm trying to Rewrite web requests using (.htaccess)
from: cobweb.seas.gwu.edu/~mpnl
to: cobweb.seas.gwu.edu/~mpnl/joomla
My latest (.htaccess) file is below:
# turn on Rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu/~mpnl$
RewriteCond %{REQUEST_URI} !^/joomla/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /joomla/$1 [L]
RewriteRule ^(/)?$ joomla/index.php [L]
You can't match against the URI path with the %{HTTP_HOST} var, only the host. You'll need to either include the ~mpnl part as a rewrite base or as part of the request URI:
RewriteEngine on
# remove the "/~mpnl" from the end of this line, add a [NC]
RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu$ [NC]
# add a "/~mpnl" to here
RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# remove the leading slash before "joomla"
RewriteRule ^(.*)$ joomla/$1 [L]
# add a condition to this:
RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
RewriteRule ^(/)?$ joomla/index.php [L]
To rewrite requests with a UserDir in the URI, you have to set a RewriteBase:
RewriteBase /~mpnl/

CodeIgniter htaccess subfolder problem

I want a second website in my domain inside the folder test
www.mydomian.com/test
Apache server running on linux.
But when I load it in my navigator styles, images, helpers can't be found.
My htaccess is like this:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Thank you in advance
I have tried what you said, placing the htaccess file you gave me in the test folder and the other one in the root directory, it didn't work.
Other options?
Try adding a .htaccess file into your test folder instead.
I use this .htaccess content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
In the folder test I would place another .htaccess, with almost the same content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/index.php?/$1 [L]

How can I disable RewriteRule for one subcategory?

I have a .htaccess in my root of website that looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.pl [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9_-]+)\.mydomain\.pl [NC]
RewriteRule ^/?$ /index.php?run=places/%1 [L,QSA]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/upload/
RewriteCond %{REQUEST_URI} !^/javascript/
RewriteRule ^(.*)$ /index.php?runit=$1 [L,QSA]
But when I type:
mydomain.pl/guests
I would like to go normally to actual folder guests. I understand that I need to somehow disable this rule for guests subfolder, but how do I do this?
EDIT:
I've included whole .htaccess file
I've put a htaccess in subfolder and added RewriteEngine Off and it works.
Add one of these
RewriteEngine On
#If the request starts with guests, then pass it through
RewriteCond %{REQUEST_URI} ^guests
RewriteRule ^ - [L,QSA]
#Or, if the request contains an existing filename or directory, pass it through
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L,QSA]