laravel subfolders .htaccess - php

I have a question (and really tried already a lot of things).
this is the folder structure on my root:
.htaccess (root)
/2014
/2015
/2016
/2017
.htaccess (2017->laravel redirect)
public
index.php
css
where 2017 has a Laravel website.
I know I need an .htaccess file inside the 2017 folder to rewrite to the public folder where the index.php file is /public
This is the .htaccess file in the 2017 folder to achieve this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
First problem I have here is that now inside my Laravel code, when I access the website via www.dotfmp.com/2017 the $REQUEST_URI contains now "2017/{page}" instead of only "{page}".
My second question is what should be my .htaccess file in the root, so that any request coming on the www.dotfmp.com website will go to /2017 folder, however www.dotfmp.com/{folder} should go to the intended folder.
An answer would be nice, an explanation even better, and a good resource the best :)
Thanks in advance
Andries

Related

.htaccess reroute to sub-directory

I have site I am running on localhost http://example and I have directory in the root that contains my api so it's http://example/api
In the api folder I have my root directory for the api which is public directory. so /api/public/....
I want to have a .htaccess that redirects the user to the public directory when they hit http://example/api/{api parameters}. So I can have a clean url without having to write this http://example/api/public/{api parameters}
I tried this but to no avail
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^api/(.*) /api/public/$1 [L]
I got the above from a SO post but still could not get it to work.
Try using:
RewriteEngine On
RewriteBase /api/public/
Quoting a stack overflow answer, "By having the RewriteBase there, you make the relative path come off the RewriteBase parameter."
With this method I believe you will have to rewrite all paths that don't fall under the base directory. If this is a problem, you could create a rule for each page.
Update: You might be able to write a rule that checked for files at that path before/after it checks for the relative path.

Remove public from URL Laravel 5.3

I'm using Laravel 5.3 is there a easy way to remove public from URL? is it necessary to add a htacces. when I change the htacces it is getting a n error like below,
copy both htaccess and index.php file from public folder to root directory and then change root index.php code as below
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.
https://laravel.com/docs/5.3/installation#configuration
Do not edit .htaccess or Laravel related files, just point web server to a public directory instead of Laravel project directory:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
Then restart Apache.
Add a .htaccess file in root which contains -
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Yes it is very simple no need to add extra htaccess,
Create a new folder in your laravel project folder.
Name it whatever you like. Example :- source
Move all the files from root into 'source' folder except 'public' folder.
4 . Move all the files from public folder to root.
5 . Change the autoload.php path
6 . Thats all. remove public folder if necessary.
In the root directory create a .htaccess file and put the following in
it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Even above solution(s) work(s) for you,but I would recommend not to do this.
Here is the reason why.
-You should be pointing your Apache host root to the $LARAVEL_PATH/public directory instead of $LARAVEL_PATH.
-you have to configure apache for user not access .env, git files , config folder's file
public directory is there for reason to make project a bit more secure. Solve the actual problem and don't try to break this simple but nice security addition.
Copy all files and folder from public folder to root folder and edit your .htaccess file as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And dont forget to change the paths in index.php file.
For details refere following links
Installing Laravel in a subfolder
https://ellislab.com/forums/archive/viewthread/151342/#734511

HTML Directory looking URL

My friend recently told me that I was able to do something like
http://example.com/about/
without having to create a directory and place a index.html inside of it, like this.
http://example.com/about/index.html
How in the world do you do this? I didn't know it was even possible to do unless you created a directory and placed a index.html inside of it, anyway thanks. :)
If you use Apache as WEB server, use .htaccess for you site root directory.
Write following rule here:
DirectoryIndex index.html
Also you can use more than one file here:
DirectoryIndex index.php index.html
In this case first found will be used.
If you don't want to use .htaccess, you can setup same in Apache config file (httpd.conf):
< Directory /foo>
DirectoryIndex index.html
DirectoryIndex index.php
< /Directory>
Here /foo - root directory of your site
Servers like Nginx can allow you to rewrite URLs, so you can place your file wherever you want.
If you use Apache web server then create a file named .htaccess
and write code like below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
If this code when you type a url like www.example.com/about its doing to open your aboutController
Write aabout in this url http://salopek.eu/content/28/create-a-simple-php-mvc-framework
If you does not use Apache or you want simple solution then then you can just create a folder named about and create a simple index.html or index.php file. Then when you type www.example.com/about
it opens your about\index.html or index.php file
I hope it helps

.htaccess for site in sub-directory using Yii framework

I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.

301 Direct from old site folders to new site structure

Thanks in advance.
I have redone my website with new folder structures
Old site structure:
www.domain.com/folder/file.aspx
New site structure:
www.domain.us/folder/file.php
Notice the new TLD. I have around 20 folders that I want to redirect. Is it possible to do this with one file or do I have to do a 301 for each individual folder from the old site to the new site? What is the best way to handle this?
Again,
Thanks in advance
You should use mod_rewrite for that. Put this in the .htaccess file in the public_html folder of your FTP:
RewriteEngine On
RewriteRule ^([^.]+)\.aspx$ $1.php [L]
This should make sure that when someone is calling Banana.aspx they get to see Banana.php.
The .htaccess file in the directory takes precedence over any .htaccess file in the parent directories. So, if you already have .htaccess file in each of your sub-directories then putting an .htaccess file in your root directory makes no sense.
Remove the .htaccess files from each of your sub-directories that you want to redirect and update the .htaccess file in your root folder. You probably need to put 20 rewrite rules like this. Keep in mind that removing .htaccecss from sub-directories will remove all restrictions that you [might] have put on that folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteRule ^folder/file\.aspx$ http://domain.us/folder/file.php [R=301,NC,L]
RewriteRule ^folder2/file\.aspx$ http://domain.us/folder2/file.php [R=301,NC,L]
301 for permanent redirect, NC for No Case, L for Last rule to process if matches
OR
Update the .htaccess file in each directory that you want to redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteRule ^(.*)\.aspx$ http://domain.us/folder/$1.php [R=301,NC,L]
#the following might also do; just test it out
#RedirectPermanent (.*)\.aspx$ http://domain.us/folder/$1.php
So,, depending on your situation, you pick one. I'd go with option one so that all redirects are in one place. You can always move the restrictions from subdirectories' .htaccess to root's .htaccess.
If you use 301 PHP header() redirect, then I'm afraid it can only be done one-by-one.
But if you use mod_rewrite to handle this for you, then this is a relevant source for you to have a look: http://www.gnc-web-creations.com/301-redirect.htm

Categories