I have the following directory structure:
/home/username/public_html
my_laravel_app/
.htaccess
app/
bootstrap/
public/
.htaccess
index.php
robots.txt
...etc (standard Laravel 4 public folder contents)
vendor/
...etc (standard Laravel 4 files and folders)
I am trying to use an apache RewriteRule to redirect a request to http://example.com/~user/my_laravel_app to http://example.com/~user/my_laravel_app/public/index.php. In the first .htaccess file (the one directly under the my_laravel_app/ directory, I have the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/my_laravel_app
RewriteCond %{THE_REQUEST} !public/
RewriteRule ^(.*)$ public/index.php [R]
</IfModule>
When I navigate my browser to http://example.com/~username/my_laravel_app, I correctly get redirected to http://example.com/~user/my_laravel_app/public/index.php, and the laravel application loads as expected displaying the route for / as defined in app/routes.php.
However, I want the redirect to be internal, meaning, I don't want the users browser to display the new location. So, I simply remove the [R] flag on the rule. The .htaccess file now contains:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/my_laravel_app
RewriteCond %{THE_REQUEST} !public/
RewriteRule ^(.*)$ public/index.php
</IfModule>
Now, when I direct the browser to http://example.com/~username/my_laravel_app I get a Laravel whoops error page saying there was a NotFoundHttpException. When I look at the error page, it seems as though the redirect is "working", because I see that the SCRIPT_NAME is /~username/my_laravel_app/public/index.php.
However, I believe the issue is possibly that the REQUEST_URI is /~username/my_laravel_app/, instead of '/', as it would be if Laravel were installed in the root of a domain like normal. So it seems as though Laravel is looking for a route match using this REQUEST_URI variable.
Is there any way to get Laravel to recognize the correct route?
I had the same issue on Laravel 4.1, and appears related to my apache config, however, I did find a workaround which was to add the following to the top of the public/index.php:
$_SERVER['SCRIPT_NAME'] = 'path/to/laravel/index.php';
// note: this is a workaround for mod_rewrite and laravel which updates the script_name from /path/to/laravel/public/index.php to /path/to/laravel/index.php. I forget exactly why this worked, but I think when I traced the problem back it was related to something in a symfony dependency.
Related
I am currently following this guide to allow the running of our current legacy PHP application inside of Laravel with a view to migrating over totally over time.
https://tighten.co/blog/converting-a-legacy-app-to-laravel/
To this end I have managed to get Laravel to run the legacy application index.php file however I believe there may be some issue with the routing as the URL is not correct and the login page does not load.
Here is my current file structure, I have two .htaccess files one at the root of laravel and one in the root of the legacy folder.
The laravel root htaccess file is rewriting the public folder so it should not show in the URL while working on dev.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
The legacy applications .htaccess file handles all the routing for the views and files of the old system, see below.
Options -Indexes
DirectoryIndex index.php
AddType image/svg+xml svg
AddType image/svg+xml svgz
# HSTS Header - removed https only as AWS lb > server is http
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# core redirects
RewriteEngine On
RewriteBase /legacy
RewriteRule ^login/?$ /views/login/index.php [QSA,L]
RewriteRule ^app-redirect?$ /callback/apps/redirect_device.php [QSA,L]
RewriteRule ^login/logout?$ /views/login/logout.php [QSA,L]
RewriteRule ^register([/]*)$ /views/login/register.php [QSA,L]
RewriteRule ^login/change-password([/]*)$ /views/login/change_password.php [QSA,L]
RewriteRule ^authenticate/auth-check([/]*)$ /views/login/auth_check.php [QSA,L]
It also appears to be affecting the public URL rewrite when accessing the application as it adds public back into the URL like so:
system.co.uk/public/login
when it should be
system.co.uk/login
Summary:
So I have it successfully running the index.php file located in the legacy folder however it just stops there it doesn't appear to be taking any of the routes in the legacy .htaccess file like /login into the account.
Does anyone have any idea what could be going on here?
the htaccess file under the legacy folder shouldn't affect the root folder.
redirects can be cached by your browser, have you tried other browser or used incognito mode?
I have a Laravel 5.2 application on a cPanel hosting account in which I had to remove the "public" folder from the URL with the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Now I need to redirect everything incoming to just the main domain/page www.example.com to be redirected to www.example.com/en.
I tried some examples but all of them break the local css and img.
Reinventing the wheel is always a bad idea. What you should do is to setup your web server correctly by pointing it to a public directory and restarting it. Also, use original Laravel .htaccess.
After that use Laravel localization.
Then you could just copy Controller#method you using for /en route and put it into / route.
I have a webapp written in Laravel that needs to run in a folder on a web host.
The app will have to be accessible via hostname.com/webhit/. This will point to the app's home page.
I only have one route:
Route::controller('/', 'HomeController');
HomeController's getIndex needs to serve the home page. This works.
However, as soon as I want to go to something like hostname.com/webhit/login, I get a 404 from Apache.
Obviously, .htaccess is not working properly. I need it to, essentially, turn URLs that look like hostname.com/webhit/login into hostname.com/webhit/index.php/login.
I have a .htaccess file in www/webhit (where index.php is located) that looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ webhit/index.php/$1 [L]
</IfModule>
I am not very familiar with .htaccess file syntax, but I believe it's doing something wrong.
Edit:
I did it. My .htaccess was wrong (it actually causes a redirect loop), but the issue was that it wasn't even being parsed by Apache (hence the 404 instead of a 500 due to >10 redirects in a request). I did the following steps in order to get everything to work:
Enable mod_rewrite and restart Apache (plenty of docs out there on how to do this)
But wait, there's more! By default, Apache on Ubuntu prevents URL rewrites. See this site. Most importantly, the following fragment from the URL above is very important: "By default, Ubuntu's Apache will ignore the directives in your .htaccess files." You will need to actually enable rewrites by editing \etc\apache2\sites-available\default and setting AllowOverride to all (see link above for more details).
Reload the configuration (or just restart apache).
Make sure you're using the correct .htaccess. My original version actually has a redirect loop in it. See the selected response for the correct version.
After this, I got it to work. I hope it helps future programmers having a similar issue!
Check default server requirements - laravel .htaccess file works for most situations. Try with this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I would suggest you to use resourceful controllers - mappings from your route to your controllers methods are much more clear, and you'll get full
resource with one command (routes,models,views,controllers)
I am running wamp on win7 just in case. I am trying to route all requests through a route.php to get clean URLs. Below is my htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(auth|folder2) [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) ./route.php?path=$1 [L]
As you can see the rule should apply to all files, and it did when I ran it on my old wamp installation. Now after copying the files to a new system, for some reason, the rule seems to work for all links except index.php. So if I try http://localhost/proj, I get no routing in the new system. Any ideas?
And before anyone asks, yes rewrite_module is on, and the htaccess is being read and executed cause it works for all other links like http://localhost/proj/users and also because if I try putting garbage values in the htaccess it throws a nice 'internal server error'.
I'm guessing you have an index.php in that folder, and this condition:
RewriteCond %{SCRIPT_FILENAME} !-f
...tells apache to perform the rewrite only if the file does not exist.
I would suggest or renaming the file, as you'll need that condition to route static assets like images, scripts, etc.
If there is no index.php (or index.htm[l]) in that folder, just define route.php as the default file in that directory. In your .htaccess:
DirectoryIndex route.php index.php index.html index.htm
(Be aware that this will also apply to any sub-directories as well)
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.