Trailing slash in url and virtual host - apache

I have a laravel project on localserver and want it to work even when the trailing slash is added to the url. It is working fine when accessed with localhost url. The htaccess is as follows:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /LaravelProject/public/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Trying to access page like
http://localhost/LaravelProject/public/login
and
http://localhost/LaravelProject/public/login/
is working fine.
However I have also VirtualHost defined like this
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/LaravelProject/public"
ServerName LaravelProject.dev
ServerAlias www.LaravelProject.dev
ErrorLog "c:/wamp/logs/apache_error.log"
CustomLog "c:/wamp/logs/access.log" common
</VirtualHost>
Via url laravelproject.dev, I can access the document root and can see the "You have arrived." But when I try to access other url say laravelproject.dev/login, it gives me 500 Internal Server Error.
If I remove the RewriteBase from .htaccess and modify the RewriteRule as
RewriteRule ^(.*)/$ /$1 [L,R=301]
then the urls laravelproject.dev/login and laravelproject.dev/login/ work fine.
If I do this, again the problem persists with localhost url with trailing slashes and the
url http://localhost/LaravelProject/public/login/ redirects to http://localhost/login.
I want to be able to use both localhost and virtualhost urls with and without trailing slashes and give the same result. As it seems, it is one or the other for me.

I don't think you need any of these lines in your .htaccess file. I have got mine set up like this on a couple of projects and it works perfectly fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Related

Laravel: Apache alias for URLs with trailing slash

I have a Laravel website in directory /home/user/Documents/laravel-training.
I would like to visit my website from http://localhost/dev/,
so I set an alias in /etc/apache2/sites-enabled/000-default.conf.
Here is my 000-default.conf:
<VirtualHost *:80>
ServerName foo.example.net
ServerAdmin foo#example.org
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /dev /home/user/Documents/laravel-training/public
<Directory /home/user/Documents/laravel-training/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I also added RewriteBase /dev to the beginning of /home/user/Documents/laravel-training/public/.htaccess.
Now, I can visit my website successfully in case the URL does not ends with a slash.
For example, if I add the following route:
Route::get('test', function() {
return view('welcome');
});
Then I can visit it from http://localhost/dev/test.
But if my URL is http://localhost/dev/test/, the server will incorrectly redirect my page to http://localhost/test, and causes a 404 Not Found error.
Not Found
The requested URL /test was not found on this server.
Apache/2.4.12 (Ubuntu) Server at localhost Port 80
Does anyone know how to fix this problem?
In response to Stefano Ortisi's comment:
I did not change anything else in .htaccess. I just kept the default settings.
RewriteBase /dev
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
It is not an error that it redirects without the trailing slash.
In your public/.htaccess as you can see there is the following lines:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
^^^ as the comment states that will redirect a url with a trailing slash if it is not a folder. If you remove that you will see it will not redirect you.

Can't get Anchor CMS rewriting to work

I'm currently attempting to install Anchor CMS on my Apache server.
I have successfully deployed it to the server. That is, when I go to www.domain.com, I am greeted with the default-themed front page and a test post :)
However, if I try to click the post, or go to the admin area, I get an error such as:
Not Found: The requested URL /posts/hello-world was not found on this server.
However, if I follow the (in this case) post link using a direct link, such as:
www.domain.com/index.php/posts/hello-world
It works completely fine. So, it appears to be a problem with the rewriting.
I found another problem on Stackoverflow exactly like mine, located here
Unfortunately, I did the same steps and nothing works. Just to recap, this is how my VirtualHost looks:
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/domain.com/public_html
<Directory /var/www/domain.com/public_html>
AllowOverride All
Require all granted
</Dictory>
</VirtualHost>
And this is how my Anchor CMS config.app file looks:
return array(
'url' => '/',
'index' => '',
...(rest of the data here)...
As the documentation states to use an empty index in this default case.
My .htaccess currently looks like this:
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
And it is located in the document root "public_html"
Sadly, even with these things configured, I still get the "Not Found" errors. I don't really know where to go from here :(
EDIT
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^ http://google.com [R,L]
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
I tried to put in a general rewrite to the .htaccess file, and it seems like it's not being executed:
It appears that the mod-rewrite is not being loaded.
See your server configuration file (httpd.conf). There should be a statement as follows:
LoadModule rewrite_module modules/mod_rewrite.so
If the line starts with #, remove the # character. You can also use the command:
a2enmod rewrite
Also, try to shift your Rewrites outside of the IfModule block.
Options -indexes
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]

Fresh Laravel 5 shows 500 Internal Server Error no matter what (OS X, Apache)

My setup is OS X Yosemite, default Apache and PHP 5.6.
Vhost is set correctly, I have two other apps running ok:
<VirtualHost *:80>
ServerAdmin polar
ServerName lara5.com
ServerAlias lara5.com
DocumentRoot "/Users/polar/Sites/lara5/public"
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
Hosts file is OK.
I've set the permissions to 777 to the whole project folder, still nothing.
If I remove .htaccess, the splash screen shows for the url lara5.com but the /home route returns 404.
I have tried both .htaccess (inside /public, ofc) Laravel provides, none of them work, 500 doesn't matter which route I access.
Default:
<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>
Other one from the docs:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Any ideas?
Thanks.
Translating comment into an answer to help out anyone else comes across this problem.
Based on your snippet from error.log it seems that Options directive isn't allowed in your .htaccess.
You can fix this error by commenting any line in your .htaccess starting from:
Options
To enable use of Options in .htaccess use:
Options All
in your Apace config's <Directory> section of your DocumentRoot.

CodeIgniter Remove index.php with .htaccess

I've been working on this for an hour already so I figured I might as well ask.
I am trying to remove the index.php from my CodeIgniter app's URL and can not do it.
The app is running on a dedicated server in my office and I access the app through the url http://smr_local
This is my base virtual host block
<VirtualHost 192.168.1.90:80>
ServerAdmin admin#server.com
DocumentRoot "/var/www/smr.dev/app"
ServerName smr_local
ErrorLog "/etc/httpd/logs/error_log"
CustomLog "/etc/httpd/logs/access_log" common
<Directory /var/www/smr.dev/app>
Order allow,deny
Allow from all
AllowOverride All
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
</VirtualHost>
Here is my .htaccess file
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
And my config file
$config['base_url'] = "http://smr_local/";
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Right now when I'm trying to access my base url http://smr_local/user/courses I get an error in my apache error log I get this.
File does not exist: /var/www/smr.dev/app/user
I really don't know what to try next. Any help would be appreciated.
Have you checked official user guide?
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
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.
I have been working with Codeigniter previously and that is how I get it to work:
Here is my .htaccess (assuming you have default folder structure):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Prevents user access to the application folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
That is relevant part of config.php:
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
If this won't help, your problem likely in Apache configuration.
Then provide your httpd.conf and any other relevant configurations, not only virtual-hosts.

RewriteRule to cause subdomain to load another DocumentRoot?

Apache (mostly) noob here... could sure use a little help with what I am trying to do with a RewriteRule in .htaccess:
...to causes URLs using our sharpedge. subdomain to auto-load from the Internet_IE directory - one level deeper than the site root.
I have this in httpd.conf:
[snip]
NameVirtualHost 11.22.33.44
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName ourdomain.com
ServerAlias www.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName sharpedge.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
[snip]
...and this in .htaccess (in the site root, here: /var/www/html/ourdomain.com/)
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$
# years ago this next line was used here, but in httpd.conf: (I don't know what it was supposed to do)
# RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^sharpedge\.ourdomain\.com(.*) /var/www/html/ourdomain.com/Internet_IE/$1 [L]
</IfModule>
..but nothing happens from the RewriteRule; it is as if the RewriteRule was not there.
I'd much appreciate any suggestions.
Edits:
I would just change the DocumentRoot to the directory I actually want to serve, but I need to be able to call (include) files from within files in that directory with a prefixing / in the include path, where those files are located in the site root. Those files are also similarly called from other files that are in the site root.
You don't want to put any part of your host in the RewriteRule, but you got the HTTP_HOST right, try:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
# to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Internet_IE/$1 [L]
Try changing the !-f and !-d checks:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/Internet_IE/
RewriteRule ^(.*)$ /Internet_IE/$1 [L]