Mod rewrite for subfolder in subfolder - mod-rewrite

this is my file structure at the web server:
DOCUMENT_ROOT/
foo/
www/
.htaccess
bar/
index.php
What i should write to foo/.htaccess, if I want to redirect everything from www.myserver.com/foo/www/ to www.myserver.com/foo/?
I tried this:
RewriteEngine On
RewriteRule ^(.*)$ /www/$1 [L,NE]
But error 404 is always shown. I tried elaborate with RewriteBase also with no success.
Thanks a lot
P.S. When www/ and .htaccess is in DOCUMENT_ROOT, it works OK. But when I put them to subfolder, always getting error 404 :-(

Yes, I want to go to http://www.myserver.com/foo/ and get served the stuff in /foo/www
This will never work if your htaccess file is in /foo/www because it won't take effect unless the request is for something inside /foo/www. The request is only /foo so the .htaccess file is ignored. You'll need to either move the .htaccess file to the document root, changing it to:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/foo/www
RewriteRule ^foo/(.*)$ /foo/www/$1 [L]
or move it to the /foo directory, changing it to:
RewriteEngine On
RewriteBase /foo
RewriteCond %{REQUEST_URI} !^/foo/www
RewriteRule ^(.*)$ www/$1 [L]

Related

.htaccess rewrite to index.php or index.html based on condition

I'm not so good with htaccess and tried to find an answer to my question but no luck so far.
So I have this .htaccess rewrite:
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
Which works well.
The website is an Angular site where I have dynamic URLs which are routed by JS.
So if I open base domain: example.com works well because index.html is served.
But if I open a route like: example.com/example-route. It says 404.
Could you please help me how should I modify the .htaccess file?
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
You would seem to just need to rewrite the request to index.html after your API rewrite to index.php. However, you should modify your existing rule to add the L flag and the regex that matches the request should be anchored (although the condition is not required at all since the URL check should be performed in the RewriteRule directive itself).
For example, try the following instead:
# "index.html" needs to take priority when requesting the root directory
DirectoryIndex index.html
# Abort early if request is already "index.html" or "index.php"
RewriteRule ^index\.(html|php)$ - [L]
# Rewrite certain requests to Laravel API
RewriteRule ^(api|nova|nova-api)($|/) index.php [L]
# Rewrite everything else to Angular
# (unless it already maps to a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Since the "homepage" is already working OK, it would suggest DirectoryIndex is already set OK in the server config (and prioritising index.html), although explicitly setting this to just index.html (as above) is more optimal, if this is all that's required.

Deploy Laravel App

I'm trying to deploy my Laravel app and block the access to the others files like .env
I put all my laravel app in the www folder, and I add this htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I go to my domain url I have all the files.. seem like my htaccess is not working (he's on the Laravel app root)
Here's a simple method using only a .htaccess file placed in Laravel's root directory - e.g. alongside app, bootstrap, config, ... No changes whatsoever are necessary to your code.
The file rewrites all the requests so that requesting /file.png would in fact return /public/file.png and anything else is routed to /public/index.php. This also ensures that nothing outside the public folder can be accessed, thereby protecting any sensitive files like .env or database/*.
The simple method
This method assumes that DOCUMENT_ROOT is set properly by your Apache server. Try this first and use the 2nd method only if it doesn't work on your server.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
The slightly more complicated method
If your server doesn't set DOCUMENT_ROOT properly, then you'll need to use an absolute path in RewriteCond. That is, an absolute path on the server's filesystem. You can get it by copying the following script to the directory where your Laravel installation will reside and visiting its URL - i.e. http://example.com/get_doc_root.php.
get_doc_root.php
<?php
echo getcwd();
This should result in something like /var/www/example.com/web. Use the following .htaccess file and replace [[your path]] with the actual path you got.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond [[your path]]/public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
In our example case, the RewriteCond line would look like this:
RewriteCond /var/www/example.com/web/public%{REQUEST_URI} -f

Redirect laravel installation in a subfolder, hiding the subfolder

Let's say i have installed laravel on a subfolder: i placed all the app stuff outside the html root, renamed the "public" directory in "laravel" and moved under the html root.
I can see the app by connecting to www.mydomain.com/laravel
I used the following .htaccess in the root html folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/laravel/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /laravel/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ laravel/ [L]
Now i can access my site directly from www.mydomain.com
But i see that i can still access it from www.mydomain.com/laravel ... and since all the links are generated starting from the base url, the menu and every anchor href points to www.mydomain.com/laravel... even the canonical link of the pages contains "laravel".
Questions:
is there a way to avoid this??
should i worry for this?
Thanks for help.
If you are using apache, you need to configure your hosts. Either http-vhosts.conf or httpd.conf or similar file.
<VirtualHost >
...
<Directory "path/to/your/root/html/laravel">
</Directory>
</VirtualHost>
So all you need to do is change the directory path to point directly to your laravel folder inside of html directory.
If you do that, remove the lines you added in the .htaccess
RewriteCond %{REQUEST_URI} !^/laravel/
...
RewriteRule ^(/)?$ laravel/ [L]
And place the .htaccess file back in laravel directory so it can serve page directly from there.

How do I exclude stylesheet and javascript files from my rewrite rules?

I have a rewrite rule currently as shown below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ $2.php?locale=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) $2.php?page=$3&locale=$1 [QSA,L]
My page URLs are like this:
http://example.com/en/new
or this:
http://example.com/en/new/1
As for the index page it's like this:
http://example.com/en/index
and I wanna get rid of the 'index' word so I added this rule:
RewriteRule ^(.*)$ index\.php/$1 [L]
Which works as expected except for my web assets (css, js files) which are located under: /css and /js folders now has 500 error. So my question is how to I exclude URLs pointing to these files under these 2 directories from being rewritten.
This solved my problem. Either use the suggested answer from here
http://stackoverflow.com/questions/1848500/htaccess-mod-rewrite-how-to-exclude-directory-from-rewrite-rule?rq=1
or simply create a .htaccess file inside the directory where you don't want rewriting to take place and put this in:
RewriteEngine Off

.htaccess URL rewriting for CodeIgniter not working

I have the following .htaccess in /home/domain/public_html/subfolder
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
RewriteRule ^(.*)$ public/index.php?/$1 [L]
But this doesn't work http://domain.com/subfolder/
File does not exist: /home/domian/public_html/public
I apparently have my rewrite rules messed up. This entire CI application is in a subfolder of the web root.
/home/domain/public_html/subfolder
[sssss#ff subfolder]$ ls
application license.txt public system user_guide
and index.php is in public.
First, your index.php should be in the same directory as application and system.
Once you moved it, try this in your .htaccess
RewriteEngine on
RewriteBase /subfolder/
RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Also on an unrelated note, I'd delete the user-guide directory since everything it contains is accessible at on the CodeIgniter Site.

Resources