CakePHP Website Path Access Issue - php

I am using CakePHP 1.3 on my LAMP Environment for one of my project, and what shocked me suddenly about my website path issue for URL:
Its coming like this :
http://localhost/project/app/webroot/index.php/mydocuments/
While till now it was coming properly like:
http://localhost/project/mydocuments/
But I really don't know, what settings altered to cause this path issue.
I have 3 default .htaccess files which CakePHP provides like my folder structure is, one htaccess file is in root folder (outside app folder), one htaccess file into app folder and one more into app/webroot folder as you've described here, but still its path is the issue..

I am assuming you use Apache and your Apache docroot is points to your app/webroot folder.
You need a .htaccess file in your webroot dir to make this work on Apache.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
If your Apache docroot is your app root (not recommended) you need another .htaccess file like this in your app root:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Hope this helps!

I second what wrdevos is saying. On top of that, also make sure that under the Directory directive of your documentroot (in your Apache config) the option AllowOverride is set to All, like this:
<Directory "/var/www/html">
AllowOverride All
# Other options should follow...
</Directory>
On most systems it defaults to AllowOverride None, which doesn't allow .htaccess files to be run at all.

Related

.htaccess - DirectoryIndex appears to have no effect

I'm trying to adapt the .htaccess file in the root of my CodeIgniter installation so I don't have to include index.php in all the URLs leading to my controllers.
This is the content of my .htaccess file:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
DirectoryIndex login.php
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteCond $(REQUEST_FILENAME) !-f
RewriteCond $(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1
As you can see, it's the default .htaccess file with some rewrite rules for mod_rewrite and a DirectoryIndex directive appended to it. Mod_rewrite is enabled (I've checked this using apachectl -M).
Now, I'm running Apache 2.4.18 on Ubuntu 16.04 LTS and I am using CodeIgniter 3.1.6. The problem is that when I type in no URL I get redirected to CodeIgniters index.php instead of my wanted login.php. I'd rather get the DirectoryIndex directive working instead of transferring the contents of login.php into index.php for obvious reasons. My mod_rewrite rules are meant to route URLs to existing files "through" index.php without having to include index.php in their URL. This way, my URLs are cleaner but I can still use CodeIgniter's facilities.
My master Apache configuration obviously allows me to use .htaccess files (since CodeIgniter needs this too) and the URL rewrite directives work just fine. Why does the DirectoryIndex not seem to do anything no matter where I place it inside the file?
Check out if you have AllowOverride instruction set to All -- it controls if .htaccess file has any power.
There are the answers on how to set it: How to Set AllowOverride all
I fixed the problem but not in an elegant way.
It appears to me that mod_rewrite just doesn't work (correctly) when you use it in a .htaccess file of a subdirectory of your DocumentRoot.
My CodeIgniter installation was in a subdirectory called "CodeIgniter" inside the DocumentRoot. I made two .htaccess files:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %1 !^index.php
RewriteRule ^(.*)$ /CodeIgniter/index.php [L]
DocumentRoot/CodeIgniter/.htaccess:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
Options FollowSymLinks
DirectoryIndex login.php
This seems to be doing the trick for me
Moral of the story: do all of your URL rewriting using mod_rewrite in the .htaccess located directly in your DocumentRoot

How to configure laravel project for production server and remove public in url

LARAVEL 5.0 | APACHE 2.4.7 | PHP 5.5.8
I know this is a pretty popular question. A lot of people saying to move public contents to root directory (which i find awkward because of security issues), rewriting the .htaccess, creating virtual host.
I tried creating virtual host because i think this is more acceptable solution.
but i'm encountering some problems with my configurations:
I'm running easyphp for the meantime, id like to test if i can get rid of the public route in url before setting this up live.
httpd.conf
NameVirtualhost *:80
<VirtualHost *:80>
DocumentRoot "${path}/data/localweb/quantum.dev/public"
ServerAdmin admin#localhost
ServerName quantum.dev
ServerAlias www.quantum.dev
<Directory "${path}/data/localweb/quantum.dev/public">
AllowOverride All
Options Indexes FollowSymLinks
Order deny,allow
Allow from quantum.dev
Deny from all
Require all granted
</Directory>
</VirtualHost>
I modified C:\Windows\System32\drivers\etc
hosts
127.0.0.1 quantum.dev
When I try to browse http://127.0.0.1/quantum.dev/ it will load the listof files and not the project inside public
http://127.0.0.1/quantum.dev/
Please revert all your changes to the directory. restore the default structure and configuration of the project. If i'm not mistaken and correct me if i am, you are using a shared hosting provider. If that is the case, here is a more proper way of doing so.
In your .htaccess, you need to add the following.
At the top of the file add this line:
DirectoryIndex public/index.php
and in here:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Change the last rule to this:
RewriteRule ^ public/index.php [L]
Now, all you need to do is to move this, and only this(the .htaccess file), to the root folder of your project. In this way, you are maintaining the laravel project structure in its original state.
put this .htaccess file near public folder
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
When you are selecting a directory for your domain in c-panel, simply provide path till the Laravel's public directory and not just till the Laravel's root directory. That way you don't need to change .htaccess at all.
I have just recently upload my site to the production and follow above steps. It worked very well.
This is my configuration that is working fine for me. So first of all, your Virtual host configuration should be like that :
ServerAdmin webmaster#domaine.dev
ServerName domaine.dev
DocumentRoot /var/www/mysiteweb/public/
<Directory /var/www/mysiteweb/>
Options -Indexes
DirectoryIndex index.php index.html
AllowOverride All
</Directory>
Anf after, make this on your .htaccess laravel file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Don't forget to check if the mod_rewrite is enable.
Hope that helped. Regards.

symfony2 - remove /web from urls

I'm managing a website written in symfony 2.2, hosted on a web hosting service.
I'm pretty much a complete neophite in web development, so I'm probably making some stupid mistake.
Anyway, I want to be able to access the site without the /web part.
Example: 'domain.com/symfony_site/web/mypage' -> 'domain.com/symfony_site/mypage'.
Let me say from the start: I cannot manually configure apache on the server, I only have FTP access to it.
I think I'm left with .htaccess files solution.
I have tried each and everyone of the solutions I found on the internet (most of them from this site) and none worked.
And yes, mod_rewrite is enabled.
I feel like the simplest solution would be to add a .htaccess file in the root directory of the site (the one which also contains the /web folder), with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/$1 [QSA,L]
</IfModule>
And this does seem to redirect properly, but then I get a "The requested URL web/mypage was not found on this server." if I visit 'domain.com/symfony_site/mypage'.
The desired home page 'domain.com/symfony_site' intead gives a "You don't have permission to access /symfony_site/ on this server.", so it looks like this is not even redirecting properly.
First, you need to move the .htaccess from the web directory to the root directory (the DocumentRoot).
Then, open the moved .htaccess, search all occurrences of /app and replace them by /web/app.
The .htaccess (without comments) should looks like :
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/web/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /web/app.php/
</IfModule>
</IfModule>
Now your application can be correctly browsed using the project root directory as DocumentRoot.
This change may involve to adapt the way of loading your assets.
NOTE: It's a quick-and-dirty alternative assuming you cannot change the DocumentRoot of your application to make it points to the web directory.
a symfony2 app is designed to have the document-root at web/
here is an example configuration for apache vhost.
<VirtualHost *:80>
ServerName your.domain
DocumentRoot "/path/symfony/web"
<Directory "/path/symfony/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
so your rewrite rule is not necessary

Laravel 5 needs index.php using Apache virtual host

When I load pages that are not root, the page won't load without index.php before the route. The error I get:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at mydomain.com Port 80
To start with I have a virtual host file containing:
//also tried adding DirectoryIndex here before <directory>
<directory /var/www/mydomain.com>
DirectoryIndex index.php
AllowOverride ALL
</directory>
and a .htacces in my public with :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have another domain with the same .htaccess and appache config on the same server and it works fine. Also I did restart apache. If I use phpinfo on my index.php/route page I see at Loaded Modules:
mod_rewrite mod_setenvif
When running the website localy with xampp everything works fine.
For hours i'm trying now but I can't fix it or find any error (logs are empty) or any solutions that I haven't tried yet.
Edit:
Im using Ubuntu Ubuntu 14.04 x64 on a digital ocean VPS. And I tried turning it on and off again (as suggested). PHP Version 5.5.9-1ubuntu4.9. I followed this tutorial to config everything (except the direcoty part). I changed the location of the apache log and a file error.log is created on the given directory but no errors are in it.
My currently appache config is : (i've triple checked the white parts where the domain name is).
When I run apache2ctl -t D DUMP_VHOSTS I get
this looks fine to me, also tried disabling the default config but it didnt help.
Note: i've replaced my real domain with mydomain.com in reality I use my real domain on these spots.
Thought how do I know for sure that the conf file im editing is the one being used by the domain?
This is the correct Alternative htaccess rule for Laravel 5 suggested to use when the default doesn't work:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The Options +FollowSymLinks I believe plays an important role because the DirectotyIndex acts like a symlink.
Also check /app/config/app.php to make sure you haven't set the Config('app.url') to contain the index.php.
If you have laravel installed in a sub-directory see this:
How can I remove "public/index.php" in the url generated laravel?
Have you tried turning it all off (shutdown), and then turning it on again?
If everything is as you say, it should work. I would try a restart to see if it changes anything.
If it doesn't, please update with what OS you are using.
I ended up deleting the complete conf file and copied it again from the one thats working. I took the default .htaccess from laravel and it worked. Unfortunately I still dont know what was wrong though. The current conf file looks like
<Directory /var/www/mydomain.com>
AllowOverride ALL
DirectoryIndex index.php
</Directory>
My .htaccess in /public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I had almost exactly the same problem today, which #dasper also helped me out on. I am using digital ocean Ubuntu 14.04 also, and I had to run the command a2enmod rewrite then reload apache2. It seemed that even though rewrite appeared to be on, it actually wasn't.
First, make sure mode_rewrite is enabled. I am sure you have probably done this but I want to make sure nothing is missed.
Next, see what version of apache you are running since the syntax has changed between 2.2 and 2.4. This should not be a big deal and highly doubt this is the culprit but could save you trouble in the future
Next, try putting the rewrite condition inside of the vhost information instead of the htaccess file. This can help a little with performance as well as give you a console warn/err when restarting apache where the htaccess errors could be squelched.
<directory /var/www/mydomain.com>
Options -MultiViews
AllowOverride ALL
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</directory>
Beyond this point I would be at a loss without some more logging or information from the server. You can also add CustomLog into the vhost and report all requests processed by the server.
The problem could be with your virtual host configuration and/or a misplaced .htaccess file.
Make sure your .htaccess file is in the public/ folder and that the DocumentRoot is set to that public/ folder.
And also, the < Directory> directive isn't controlling your virtual host but rather controls the folder itself and how it can be (or can't be) accessed on the server. To change the configuration of the virtual host do so inside the < VirtualHost> directive.
Here is an example of a virtual host configuration:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/var/www/mydomain.com/public"
DirectoryIndex index.php
</VirtualHost>
Here is the default Laravel Apache config:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and here is an alternative:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Try to rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.
Solution found here
I believe your directory path is wrong. You need /public on the end because Laravel treats that directory as the web root. Try this:
<directory /var/www/mydomain.com/public>
DirectoryIndex index.php
AllowOverride ALL
</directory>
But the better solution is to put the contents of the .htaccess file in your virtual host. Like this:
<VirtualHost *:80>
DocumentRoot "/var/www/mydomain.com/public"
ServerName mydomain.com
ServerAlias *.mydomain.com
<Directory "/var/www/mydomain.com/public">
# Ignore the .htaccess file in this directory
AllowOverride None
# Make pretty URLs
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
Also, you should make sure your config file is loaded by running:
apache2ctl -t -D DUMP_VHOSTS
This will print a list of all the loaded vhosts. If yours isn't in that list then make sure it is enabled by running:
a2ensite [name_of_your_config_file]
Then restart apache with:
service apache2 reload
One more note: don't forget to edit /etc/hosts and add 127.0.0.2 yourdomain.com

How to run laravel in root directory without the public folder?

I am running the latest version of laravel on my own server running Debian and Apache2.
The contents of laravel are located in /var/www which is what my domain name is pointed to however all of the functionality happens through the public directory so I would have to go to http://mydomain.com/public to access anything.
I would like to change it so that I only have to access http://mydomain.com. Is this possible?
How can I change this? Would I have to move everything up another level to the parent at /var?
I haven't found anything online so far that says that it is possible, or that it is a good idea.
That is not your problem, you need to point the virtual host to the public folder.
The following worked for me, as discussed here: https://stackoverflow.com/a/16569078/3091980
Move all contents of the /public folder down a level. Then update the include lines in index.php to point to the correct location - if it's down a level, remove the "../".
Sorry I deleted my old answer .. that was if u rename server.php to index then it will work but of course not because laarvel not load all service. so you have to paste into root all public folder file. and remove "../" from everywhere in index.php
Add a file in laravel root directory, name it as .htaccess and put this inside,
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
It will be fixed surely.
Locate your httpd.conf and change the part that DocumentRoot "/var/www" to "/var/www/public" and override directory to allow .htaccess file of laravel
...
DocumentRoot "/var/www/public"
...
<Directory "/var/www/public">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
...

Categories