Cannot properly merge two .htaccess - apache

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]

Related

.htacces rewrite all to index / ignore images and css files

I use the following .htaccess file to send all my requests to the index file.
But now I would like to ignore everything that comes from the /static folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
So here's the thing:
- index.php
- static/
----img
--------bg.jpg
----css
--------main.css
Currently it is so that on subpages of the style and the images can no longer be loaded. And I would like to renounce the use of complete paths.
You may try this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !/static/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Using THE_REQUEST is preferred for negative conditions as THE_REQUEST variable is not overwritten with other rules.
Well, you need to add an exception, right?
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

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 RewriteCond taking me to 404 - folder exists

I have two htaccess rewrite conditions:
RewriteCond %{HTTP_HOST} ^(www\.)?gotham.rentals$
RewriteRule !^gothamrentals/ /gothamrentals%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^(www\.)?sofla.biz$
RewriteRule !^southflorida/ /southflorida%{REQUEST_URI} [L]
The first one works, the second one does not (404 from the server). What gives? The folder southflorida exists and has the same permissions as the folder gothamrentals.
The answer I used was adding:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
To the southflorida directory. I suspect, maybe "/" was getting added to the url and the child htaccess was taking care of gothamrentals.

Apache rewrite empty regex (root)

I've got Apache 2.4.4 running on Windows 8 laptop (WAMP server) and I found a really odd behavior of .htaccess mod_rewrite rules.
I want to redirect the root of my website to a specific file. My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteRule $^ /static/home.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]
This works only if there's no index.php file in root directory. When I create index.php file, Apache redirects empty path straight to the index file and doesn't bother with the first RewriteRule.
Is there a way to have both these RewriteRules and index.php file working together? In oher words, my example works but I want to rename router.php to index.php and keep both RewriteRules working.
Thanks!
You can just tweak your regex pattern to work in both situation:
DirectoryIndex something-else.php
RewriteEngine On
RewriteRule ^$ /static/home.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

mod_rewrite rules in .htaccess

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]