mod_rewrite rules in .htaccess - apache

I want to do change my document root using rewrite rules in my .htaccess file, and also redirect all requests to index.php except static resources
In other words:
if a file with the path prepended with www exists (i.e. www/{%request_uri}):
http://domain.com/css/main.css should be rewritten to http://domain.com/www/css/main.css
if that file does not exist, redirect to the app main entry point www/index.php:
e.g.
http://domain.com should be rewritten to http://domain.com/www/index.php
http://domain.com/hello should be rewritten to http://domain.com/www/index.php/hello
http://domain.com/hello?a=1 should be rewritten to http://domain.com/www/index.php/hello?a=1
I tried variations, however either it gives us 500 internal server error, or infinite loop. Here is one that gives 500 internal server error:
# try to find it in www/
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -f
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -d
RewriteRule ^(.+) www/$1 [L]
RewriteRule ^(.*) www/index.php/$1 [L]
This one also:
RewriteCond %{REQUEST_URI} !www/
RewriteRule (.*) /www/$1
RewriteCond {%REQUEST_FILENAME} !-f
RewriteCond {%REQUEST_FILENAME} !-d
RewriteRule www/(.*) /www/index.php/$1 [PT,L]
Can anyone help?

I think that's impossible with a single .htaccess but it's possible with two.
You need one in your root directory, /.htaccess:
RewriteEngine on
RewriteBase /
# We don't want infinite rewriting.
RewriteCond %{REQUEST_URI} !www/.*$
RewriteRule ^(.*)$ www/$1 [L]
And another in your www directory, /www/.htaccess:
RewriteEngine on
RewriteBase /www
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Well actually I made a stupid mistake! The % sign should be outside of the {} bracket!
This can be done in one .htaccess in root dir:
RewriteEngine on
RewriteCond %{REQUEST_URI} !www/
RewriteRule ^(.*)$ www/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^www/(.*)$ www/index.php/$1 [L]

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.

Apache rewrite /product/ to /products/ and index.php

I've been asked to help on an ExpressionEngine site that's been developed. There is an existing .htaccess file that rewrites anything that isn't a file or directory so that it goes via index.php:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
This works fine, and an example URL might be:
www.mysite.com/products/foo (plural 'products')
However, It also needs to deal with old URLs which will be in the form:
www.mysite.com/product/foo (singular 'product')
I've tried the following:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+) /index.php/products/$1 [L]
RewriteRule ^(.*)$ /index.php?$1 [L]
Hoping that if it finds 'product' then it rewrites to 'products' via index.php and then quits, but it's not working.
At the same time, I need any incoming URL that starts:
/department/foo
to be rewritten to:
/departments/category/foo
Any help greatefully received.
You can use add a rule above your existing rule to for mapping product -> products:
RewriteEngine On
RewriteBase /
RewriteRule ^product(/.*)?$ products$1 [L,NC]
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

htaccess point request to sub-directory if file doesn't exists and does under the subdirectory

I'm having a really hard time with trying to get this to work so I have a sub-directory example.com/projects with a directory under that called test and I'd like it if someone typed example.com/test it would show the file from example.com/projects/test but if the file does not exist I would like it to post a 404 error.
Additionally if possible I would like any time example.com/projects/test was typed it would redirect to example.com/test instead but if someone goes to just projects/ I want them to be able to stay there.
I have tried the following .htaccess files so far
This works almost perfectly passing through the files unless the file doesn't exist in which it throws a 500 Internal Server Error
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ projects/$1 [PT]
I thought something like this might work to only pass through if the file existed under projects but doesn't work at all:
RewriteCond projects/%{REQUEST_FILENAME} -f
RewriteCond projects/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ projects/$1 [PT]
For the second part pointing anything that goes to /projects/test to strip /projects I tried this (placed before the code above)
RedirectMatch ^/projects/(.+)$ /$1
Only to find a redirect loop. How can i prevent this?
My current .htaccess file for reference:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$
RewriteRule ^/?$ "http\:\/\/example3\.org" [R=301,L]
#RedirectMatch ^/projects/(.+)$ /$1
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ projects/$1 [PT]
You can use these rules in your root .htaccess:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /projects/(\S+)\sH [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -d
RewriteRule ^(.+)$ projects/$1 [L]

Apache .htaccess retry other directory if file not found

i need a htaccess rewrite rule for my apache webserver that searches in other directories if a file has not been found.
if
http://domain.com/x.jpg
is not found, look in
http://domain.com/bla/x.jpg
if not there then look in
http://domain.com/foo/x.jpg
and so on.
i am trying the following but it doesnt work. still gives me 404s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*\.(gif|png|jpg|jpeg|apk|bmp|webp))$ /foo/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*\.(gif|png|jpg|jpeg|apk|bmp|webp))$ /bla/$1 [L]
The problem with you're current config, is that it always does the first rewrite if the file doesn't exist and the second rewrite never get's executed. You will need to check for existence of the file before doing the redirection such as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)(/.+\.(gif|png|jpg|jpeg|apk|bmp|webp))$
RewriteCond %{DOCUMENT_ROOT}%1/bla%2 -f
RewriteRule . %1/bla%2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)(/.+\.(gif|png|jpg|jpeg|apk|bmp|webp))$
RewriteCond %{DOCUMENT_ROOT}%1/foo%2 -f
RewriteRule . %1/foo%2 [L]

Cannot properly merge two .htaccess

There are 2 .htaccess files, one in / and the second in /web folders.
In a root folder (/), .htaccess looks like
RewriteEngine on
RewriteRule ^$ web/ [L]
RewriteRule (.*) web/$1 [L]
In /web folder, .htaccess looks like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
It works perfectly.
All I want is to merge those conditions and put them inside a single .htaccess that would be in root directory (/).
1 - I've tried to add RewriteBase, like
RewriteBase /web
2 - I've tried to replace conditions that aware of web folder
RewriteRule ^(.*)$ web/index.php/$1 [QSA,L]
3 - I've tried to simply append a config from one file to another with slight alterations.
But none of this works - It gives internal 500 server error.
How a proper merging should be done in this case?
This .htaccess (in the root folder) should match to your needs :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/index.php/$1 [QSA,L]
If you're putting all the rules in your document root (/) then try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/index.php/$1 [QSA,L]