.htaccess - Need help working with it - php

So today I realised my VPS still had 3 domains pointing to the origin IP - I have never worked with Apache conf files and have just attempted to add my own .htaccess from my own research:
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.infamystudio.com$
RewriteRule ^/?(.*) https://www.infamystudio.com/$1 [QSA,R=301,L]
This was placed into the var/www folder and has had no effect on rewriting other domains pointing to the origin back to my IP.
Is there any good tutorials or can anyone help me write a rewrite so I can stop this.

Your .htaccess file should be in the root folder of your website, but it will only be used by Apache if the Virtualhost configuration allow a override. So because I don't know what kind of VPS you have you have to find it yourself.
In Debian like servers this is usual in /etc/apache2/site-enable/<virtual-host>.conf (all files in site-enable are only links to the the files in the folder site-available)
Allowing rewrite for your VirtualHost
<Directory "/var/www/path/to/public">
AllowOverride All
</Directory>
If this step is missing, nothing in your .htaccess file will work. If you have already a Directory-Direktive just only add AllowOverride All You may want to restrict this later but just for testing ALL would be good.
You also can have multiple Virtual host configurations for different IP or domain names.
https://httpd.apache.org/docs/current/vhosts/examples.html

Related

Cakephp assets not fetching from webroot folder.. missing js & css,

I've been building on localhost and all this stuff works perfectly. Now trying to load the site on a shared host. I've worked through most of the issues and actually have a working site but without any css.
Layout:
My app is in: /home/cake/app
public_html is in: /home/public_html
In public_html/index.php, the only way I was able to get rid of missing file errors was to do this...
require '../cake/app/webroot' . DIRECTORY_SEPARATOR . 'index.php';
The .htaccess in public_html:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
My app was baked from the command line.
All of the index.php and .htaccess files up through the chain are untouched.
/cake
/cake/webroot
/cake/app
/cake/app/webroot
It just can't find the path to all the css and js files.
in my default.ctp, I used the standard html helper links.
echo $this->Html->css('default');
I'm at the end of the proverbial rope. Any help appreciated.
On localhost, I point the apache directory at /cake/app, but I'm pretty sure I don't have access to apache config files on a shared host, hence the reason I pointed the public_html index.php at /cake/app. Probably not right, but it felt like I was moving in the right direction since the site started working.
All your CSS & JS should be inside the app/webroot directory.
It sounds like you've setup your virtual hosts incorrectly. (This is why the CSS works in public_html but not in the webroot directory).
Basically, We only allow access to our application through app/webroot/. This will load the index.php inside the webroot which is provided by cakePHP to load the controllers for every request.
Your virtual host file should look like this:
<VirtualHost *:80>
# Correct: Notice the "/app/webroot/"
DocumentRoot "/path/to/app/public_html/app/webroot/"
# Below is INCORRECT
# Incorrect: DocumentRoot "/path/to/app/public_html/app/"
ServerName yourdomain.com
</VirtualHost>
Now.. the ONLY directory accessible from the outside world is everything in webroot, this can be JS, CSS, Images, Files or whatever other assets you require.
This is how it should be setup, you dont want people to be able to access files outside of your webroot (ie all other CakePHP files).
On shared hosting providers, you will require a slightly different setup (you wont have access to the vhosts of the shared server). This explains the slightly different directory structure the OP has said. Read here for more info on deploying cakephp on a shared host.
http://bakery.cakephp.org/articles/gedm/2009/08/29/installing-cakephp-on-shared-hosting
Instead of including the index.php from the webroot in another index.php (inside your public_html), consider changing the webroot folder entirley to your public_html.
View here for more info on change cakephp webroot folder: CAKEPHP - Change default path to webroot
Change AllowOverride none to AllowOverride FileInfo in /etc/apache2/sites-available/default.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /home/user/app/
<Directory />
Options FollowSymLinks
AllowOverride FileInfo
</Directory>
<Directory /home/user/app/>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
It worked for me.
Finally this one was the command wich worked for me to enable mod_rewrite:
$ a2enmod rewrite
Well, not sure whether this is all "right" or not, but I copied all the folders with the css and js files into public_html, which kind of makes sense. All of those assets need to be publicly accessible. Site works now.

Laravel Route not working with Wamp

As the title states, I cannot get my site to redirect. I've made several attempts and am about to lose it.
My .htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
I have this file placed in the www/Laravel/public folder as well as www directory
mod_rewrite is enabled
AllowOverride is set to All
My host file is as follows
127.0.0.1 localhost
Here is the code for route contained in routes.php
Route::get('authors', array('uses'=>'authors#index'));
I have a controller named authors.php, here is the code
class Authors_Controller extends Base_Controller {
public $restful = true;
public function get_index () {
return View::make('authors.index');}
}
I am using Windows 7 Ultimate. I'm about to blow my brains out. Please help me, I suck.
If your laravel project is located in c:\wamp\laravel\ then you should be able to call
http://localhost/laravel/public
and see the default laravel welcome page or your own.
If thats the case, you have to check two thins:
Open your /app/config/app.php file and check that the value of url directs to your project.
'url'=>'http://localhost/laravel/
Then open your .htaccess file and add the RewriteBase rule under the RewriteEngine On Line
RewriteBase /laravel/public/
Hope that helps! :)
I had the same problem. I used the solution above and it worked.
Apache rewrite_module must be enable in wamp as well.
You need to setup a virtual host pointing to your public directory...
in httpd.conf file.
Which can be found under the Apache menu...
Like so...
<VirtualHost *:80>
DocumentRoot c:\wamp\www\laravel\public
ServerName testing.com
</VirtualHost>
And your hosts file will reflect the url you choose...in this case...testing.com
127.0.0.1 testing.com
Checking rewrite_module
Go to
wamp icon
Apache
Apache modules
check rewrite_module
And cheers , there you are after some headache
Apache rewrite_module must be enabled in WAMP as well. It works with my wampserver.
Actually it has a pretty easy solution. All you have to do just check the rewrite module in apache. Check the steps in image
laravel problem with wamp server solution
Create an index.php file in the root.
<?php
header("refresh: 0; http://localhost/appnamehere/public");
?>
If you have multiple sites under one domain, you may not be able to change your public webroot.
I had all the correct settings, and the right .htaccess files in the right place, but it would not redirect. This fixed it for me.
This article was helpful in getting the wampserver/wampmanager links working.
Project Links do not work on Wamp Server
Post Script:
If you add to the virtual host file at:
E:\wamp\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf
and you create a virtual host that points to the public directory like this:
<VirtualHost *:80>
ServerName Blog
ServerAlias Blog
DocumentRoot E:/wamp/www/Blog/public
<Directory "E:/wamp/www/Blog/public/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
#
and you add to C:\Windows\System32\drivers\etc\hosts
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
127.0.0.1 Blog
::1 Blog
You do not need the index.php file I suggested. That file works if you are NOT creating a virtual host, both are not needed. It's one or the other (although it does no harm to have it).
They only right way to do this is to point web server to public directory. For example, if you've installed Laravel in C:/xampp/htdocs/ directory, you need to use these settings:
DocumentRoot "C:/xampp/htdocs/public"
<Directory "C:/xampp/htdocs/public">
Never edit .htaccess inside public folder. If you did this, you need to get original file back. Restart Apache after you make these changes.

What is Options +FollowSymLinks?

I am using a Lamp server on my computer. I started to use Laravel php framework.
In my .htaccess , If I use Options +FollowSymLinks , I get 500 error.
And If I comment out , I have to use index.php in my all addresses ..example:
/~ytsejam/blog/public/index.php/login
I use Arch Linux . Is there a way to solve it?
edit: I solved this by using virtual hosts. And deleting index.php from application/config/application.php in laravel folder.
You might try searching the internet for ".htaccess Options not allowed here".
A suggestion I found (using google) is:
Check to make sure that your httpd.conf file has AllowOverride All.
A .htaccess file that works for me on Mint Linux (placed in the Laravel /public folder):
# Apache configuration file
# http://httpd.apache.org/docs/2.2/mod/quickreference.html
# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Hope this helps you. Otherwise you could ask a question on the Laravel forum (http://forums.laravel.com/), there are some really helpful people hanging around there.
Parameter Options FollowSymLinks enables you to have a symlink in your webroot pointing to some other file/dir. With this disabled, Apache will refuse to follow such symlink. More secure Options SymLinksIfOwnerMatch can be used instead - this will allow you to link only to other files which you do own.
If you use Options directive in .htaccess with parameter which has been forbidden in main Apache config, server will return HTTP 500 error code.
Allowed .htaccess options are defined by directive AllowOverride in the main Apache config file. To allow symlinks, this directive need to be set to All or Options.
Besides allowing use of symlinks, this directive is also needed to enable mod_rewrite in .htaccess context. But for this, also the more secure SymLinksIfOwnerMatch option can be used.
How does the server know that it should pull image.png from the /pictures folder when you visit the website and browse to the /system/files/images folder in your web browser? A so-called symbolic link is the guy that is responsible for this behavior. Somewhere in your system, there is a symlink that tells your server "If a visitor requests /system/files/images/image.png then show him /pictures/image.png."
And what is the role of the FollowSymLinks setting in this?
FollowSymLinks relates to server security. When dealing with web servers, you can't just leave things undefined. You have to tell who has access to what. The FollowSymLinks setting tells your server whether it should or should not follow symlinks. In other words, if FollowSymLinks was disabled in our case, browsing to the /system/files/images/image.png file would return depending on other settings either the 403 (access forbidden) or 404 (not found) error.
http://www.maxi-pedia.com/FollowSymLinks

localhost .htaccess document root

I have an htaccess file with a simple rewrite rule.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1
On my web server, the index.php file is in /var/www, and it works normally.
On my local host, the index.php fle is in /var/www/projects/porto and while the file runs normally, no linked content ( images, css, etc ) is loaded.
I am totally new to htaccess files. Any ideas on how to work around it so I can do my testing in my locan environment?
All I've tried is changing my .htaccess file to
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/?$ /running/Porto/index.php?page=$1
But no luck.
Okay, so continuing our discussion on setting up a VirtualHost on your local machine/environment, it is roughly the same as on your other server. If you can copy the Apache configuration file from the web server then that will be a good start. At a minimum you will need something like this:
<VirtualHost *:80>
ServerName localdevsite.com
DocumentRoot /var/www/running/Proto
</VirtualHost>
(This assumes that somewhere in the file you have NameVirtualHost *:80)
The one additional thing you will need to do in your local environment is add a hosts entry:
In Linux you usually do this in /etc/hosts with something like:
127.0.0.1 localdevsite.com

What should I use .htaccess for in Zend Framework 1.9.5 application?

I've written several Web MVC apps but never before in PHP, and I'm trying to figure out the best way to work with Zend Framework v1.9.5.
I'm confused about the role of .htaccess in a Zend Framework 1.9.5 application. I have consulted many tutorials, books, and SO questions on Zend Framework (but most of them are for v1.8 at newest) and they all talk about the central role of the .htaccess file. I gather that .htaccess can be used to support virtual hosts, URL rewriting, and to allow Apache to serve static files without going through index.php but I'm not sure if this is current practice or still necessary in v1.9.5.
Currently I have written a couple of pretty simple (HTML, CSS, jQuery) Zend Framework apps and created Apache virtual hosts for them in a test Ubuntu Server 9.10 environment. I didn't use any .htaccess files at all and the apps seem to work fine.
Here's what I did so far:
I created my apps using Eclipse/PDT and the zf.sh tool. I added css, images, and js directories to the public directory that 'zf create project' produced. These apps run fine on my local MAMP installation.
On the server, I installed the Zend Framework in /usr/local/Zend/share/ZendFramework-1.9.5 and added /usr/local/Zend/share/ZendFramework-1.9.5/library to 'include_path' in php.ini.
I copied the apps to the server directories /home/myadmin/public_html/domain[12]/com.
I created virtual hosts by adding entries in the Apache available-sites directory as outlined in Slicehost Virtual Host Setup. I assign DirectoryIndex = index.php and DocumentRoot = /home/myadmin/public_html/domain[12]/com/public. Both apps seem to work fine.
Do I need to use .htaccess files? For what?
*** EDIT - My Solution
Based on Richard's link, I moved the rewrite statements that usually live in .htaccess into my virtual host definition, and now my applications don't use a .htaccess file. My domain1.com virtual host file looks like:
<VirtualHost *:80>
# Define admin email, server name, and any aliases
ServerAdmin webmaster#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
# Only serve files from inside this directory
DocumentRoot /home/myadmin/public_html/domain1.com/public
# Directly serve any requested files that exist in DocumentRoot;
# otherwise redirect the request to the index.php script
RewriteEngine off
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</Location>
# Set up log file locations
LogLevel warn
ErrorLog /home/myadmin/public_html/domain1.com/data/logs/error.log
CustomLog /home/myadmin/public_html/domain1.com/data/logs/access.log combined
</VirtualHost>
One primary use of .htaccess for zend framework and most php frameworks is to redirect all requests (except to static files) to a bootstrap file. You don't necessarily need it but your URL would end up looking something like /index.php/controller/action as opposed to /controller/action
You could also just add the rewrite rules to you apache config directly.
The VirtualHost configuration and .htaccess file are two alternatives for URL rewriting in Zend Framework. They perform the same function and you don't need both.
URL rewriting simply directs all requests for non-static files to your index.php.
Routing is the process of decomposing the request URI and figuring out which module/controller/action should be executed. This is a related but separate function to URL rewriting.

Categories