Change document root with htaccess - .htaccess

I made a router using a /public folder.
This folder contains my index.php that routes every route i need plus some css, js and stuff.
Using Wamp and a command line i can change the root folder to be /public
php -S localhost:8000 -t public
My job's done and i want to upload my newly created website to a server.
And... Yeah, i need to configure a .htaccess.
So, after some researches i found a "universal" solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]
I needed to change the last line:
RewriteRule ^(.*)$ /public/index.php [L]
Everything routes perfectly, the only problem is that css/js doesn't load. Looking at the superglobal $_SERVER it appears that
["DOCUMENT_ROOT"]=> string(15) "/home/routerphp"
does not contain my public folder.
I also tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php
With the same result.
I'm kinda desperate. Any help is appreciated.

Sometimes you just need to take a break and clear cache.
Everything is alright...

Related

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

Is there a way to properly redirect or rewrite vistors in my drupal 8 website to a subfolder?

I have the following code in my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysitedomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /web/$1 [L]
I'm currently using CPanel and CPanel does not allow you to set a new Document Root. So have my drupal files in my CPanel "public_html" directory with the composer.json files etc..and web directory that has all the Drupal related files. I am trying to get the site vistors to rewrite or redirect to www.mysitedomain.com/web for subsequent pages.. I tried the code below but does not seem to work.. Am i missing something?
To be specific.... I need the site to 1. load www.mysitedomain.com/web when www.mysitedomain.com is requested. 2. And ensure /web is is front of every subsequent request page request within the site (ie. www.mysitedomain.com/web/products should load instead of www.mysitedomain.com/products)
I am not sure what your question is, but I think you need something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
What it does: If the URL does not resolve to a file or directory in the present directory, put index.php in front of it.
I'd assume your index.php would then include the Drupal framework.

.htaccess works in the localhost webroot. How to do also works in a localhost subfolder?

A development proyect in http://localhost/myfolder has references to /assets/css/styles.css which is the site in production.
I want to call it like: http://localhost/myfolder/
I have this .htaccess in apache webroot folder c:\wamp\www:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /myfolder/$1 [L,QSA]
And it works ok when is, as I said in webroot folder.
When I move this .htaccess to /myfolder/.htaccess it does nothing.
What I have to change in this .htaccess to work also inside myfolder?
Thank you
Because it's imposible to manage absolute paths refereds to root from a subfolder (as Anubhava said), I decide to focus the problem with an /.htaccess and a RewriteCond that work only when we request the specific subfolder.
SOLUTION: (/.htacces finally is in root folder).
RewriteEngine on
# ConfiguraciĆ³n Myfolder
RewriteCond %{REQUEST_FILENAME} !-f
# Fix is the line below ---- FIX ------
RewriteCond %{REQUEST_URI} ^/myfolder/.*$
RewriteRule ^(.+)$ /myfolder/$1 [L,QSA]
In the other way, without the rewrite condition, it was affecting all requests.
This is better fix than the subfolder one, because you can download the production .htaccess and have no need to touch it.
Hope this help to others.

Modify .htaccess for codeigniter URLS

I am trying to remove the index.php from the url in codeigniter. Rewrite mod scripts are all over the place but I can't find the .htaccess file!! I tried looking in the root directory and everywhere else, but no luck.
From what I read it should be in application folder and when I go there i find the .htaccess file and all it has is deny from all. This is not the same content every one else is sharing online before modification.
Please advise.
Actually you don't find it; you create it along side with your index.php file with this contents:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php,
images, and robots.txt is treated as a request for your index.php
file.
Reference.
The solution is as follows ( for future references)
Create the .htaccess file in the root of the codeigniter application (where you got system, application, etc folders).
Paste this code in it:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open config.php and change index = "index.php" to "". Things should work now.

Why am I getting a 404 error with this routing?

In my web root (var/www) I have the following in my .htaccess file:
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|vges|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]
I then have a subdirectory (/var/www/test) with a codeigniter project. It also has a .htaccess file with the following:
RewriteEngine On
RewriteCond $1 !^(index\.php|quiz|vges|buddy|css|fonts|img|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
When I visit localhost I can view the test directory and when I go to the test directory I'm displayed with the default controller view. But when I visit localhost/test/controller I get a 404 not found error:
The requested URL /index.php/events was not found on this server.
Edit: I found the solution by changing the last line /var/www/vges.htaccess to:
RewriteRule ^(.*)$ test/index.php/$1 [L]
Although I suspect if I was to upload this test project to a server, where it is not a subdirectory then I may get an error. What's the best solution to this? What should
I change in my .htaccess file so
I can have subdirectories on my local machine
When I upload them as individual projects (such as the test project) to a server I don't have to modify the .htaccess file.
Thanks.
First, .htaccess files apply to the directory they're in, and every child directory. Your CodeIgniter-specific rules should be in the var/www/test/ CI directory -- usually wherever CI's index.php file is.
Second, your root .htaccess is kind of weird. RewriteCond conditions accumulate until there a RewriteRule rule fires, then they are reset. Your two %{REQUEST_URI} conditions conflict with each other, since the URI can't start with both. I'm not really sure what this .htaccess rule is doing, but if your URLs starting at the root have no bearing on your CI application, I don't think it's necessary to have it there in the first place. I can't say for sure without knowing your directory structure and how you want your website to function.
kjetilh is right - unless your environments share exactly the same settings, it's unlikely an .htaccess will be universally functional. Your best bet is to start them as simple as possible, and modify as necessary. A good starting .htaccess for CI 2.1.x is something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You can add your assets directories or any paths that you don't want redirected in the third RewriteCond (remember to properly escape regular expression characters such as periods). A RewriteBase rule definitely comes in handy if CI is in a subdirectory. You also don't need the system and application folder references with 2.1.x, since those folders have their own .htaccess files blocking access to them.

Resources