I know, there are thousands of .htaccess topics out there but I'm looking for days now and can't find an answer.
I want to use .htaccess to redirect from e.g. https://myserver.com/project/about to https://myserver.com/project/about.php using mod_rewrite.
Project structure
in my /var/www/project folder there is
an about.html containing nothing more than a simple <h1> Header
a .htaccess file containing
RewriteEngine on
RewriteRule ^about$ about.html [NC]
I can access the file with https://myserver.com/project/about.php.
What I've tried so far
I've looked at my /etc/apache2 folder (I'm running an ubuntu server, apache v. 2.2.22).
In the internet I've found I should add AllowOverwrite All somewhere but I couldn't find out where. I've found alot of places where it should go in the internet and tried most of them - without success.
Somehow my files all differ from others.
I've also tried to write in the first line of my /var/www/project/.htaccess deny from all or just random things to see if an error 500 gets printed. Nope. No feedback from my .htaccess at all.
Files
/etc/apache2/..
apache2.conf: Containing no <Directory> tag.
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>
httpd.conf: Empty.
conf-available/php5.6-fpm.conf: Containing no <Directory> tag.
sites-available/default: Empty.
sites-enabled/default.conf:
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
sites-enabled/ssl.conf:
<VirtualHost *:443>
...
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
...
</VirtualHost>
I hope you can help me because I literally have no single idea anymore.
Related
I currently have a variable host setup using dnsmasq on OS X 10.14. My base folder is ~/Sites. I can have a folder ~/Sites/{whatever} which I can load via http://{whatever}.test/. I am trying to use mod_rewrite for one of my projects, but RewriteEngine is not allowed for some reason.
I have my httpd-vhosts.conf file setup like so:
<VirtualHost *:80>
VirtualDocumentRoot "/Users/{Username}/Sites/%1"
ServerName sites.test
ServerAlias *.test
Options Indexes FollowSymLinks
<Directory "/Users/{Username}/Sites/%1">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and my .htaccess file like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^read$ read.php
RewriteRule ^read/$ read.php
RewriteRule ^read/([\w\s]+)$ read.php?s=$1
RewriteRule ^read/([\w\s]+)/$ read.php?s=$1
RewriteRule ^read/([\w\s]+)/(\d+) read.php?s=$1&p=$2
It doesn't matter what I have in my .htaccess file, as long as RewriteEngine On is there it errors out.
While using %1 in my Directory tag I get the following error:
/Users/{Username}/Sites/reader/.htaccess: RewriteEngine not allowed here
I can't seem to find a way around this without hard coding the directory, but that defeats the purpose of the variable host setup.
If I do hard code the directory the site works just like it should - so everything else is working.
Solved
I managed to figure it out.
In my httpd.conf I had this:
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
Notice the commenting out.
I changed it to this:
<Directory />
AllowOverride All
Require all granted
</Directory>
and it now works.
<Directory "/Users/{Username}/Sites/%1">
%1 is not valid syntax here. I don't think you necessarily need to be specific, as the directory being accessed is already controlled by the (virtual) document root. So, you could, in theory use the <DirectoryMatch> directive instead and providing it matches the appropriate directory pattern it would be sufficient.
HOWEVER, the AllowOverride directive is not permitted in <DirectoryMatch> containers, only in non-regex <Directory> containers, so this would restrict you to do something like the following as a workaround:
<Directory "/Users/{Username}/Sites">
AllowOverride All
</Directory>
<DirectoryMatch "^/Users/{Username}/Sites/[a-z]+">
Require all granted
</DirectoryMatch>
[a-z]+ will match "reader".
UPDATE:
I changed it to this:
<Directory />
AllowOverride All
Require all granted
</Directory>
and it now works.
This enables access to the entire server - which is undesirable (and the Apache docs specifically warn against doing this for security and performance reasons.)
You should be as restrictive as possible - as above (which is the idea behind using the restrictive <Directory> container inside the vHost in the first place).
I've deployed a new laravel installation to my server, While doing so I configured my apache2 as following:
I added 000-default.conf file in /etc/apache2/sites-available/ as following:
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
I've my laravel installed into /var/www/html/stellar folder now I access my installation through:
http://52.39.175.55/stellar/public/
But while calling the routes it is not working, Like
http://52.39.175.55/stellar/public/oauth/token
Here is the screenshot:
But suppose I call through this:
http://52.39.175.55/stellar/public/index.php/oauth/token
I get the access,
I tried changing my public/.htaccess file to something like this:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /stellar
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Still there is no change, Help me out in this.
Your VirtualHost on /etc/apache2/sites-available/ should look like this:
<VirtualHost *:80>
DocumentRoot "/var/www/html/stellar/public"
ServerName yourdomain.com
<Directory /var/www/html/stellar/public>
AllowOverride All
</Directory>
</VirtualHost>
Then restart apache2 and it should work.
I hardly recommend you to duplicate the 000-default.conf file, rename it and include the VirtualHost, then enable it with a2ensite command, just because it's more easy to manage.
I'm using apache 2.4.7
This is my virtualhost's config:
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
In my /var/www/login directory, I have a directory called freeseat and in there, there's the index.php file, so it's full location is /var/www/login/freeseat/index.php
When I try to access it through the following link (domain replaced), it redirects to the same URL with a trailing slash:
http://login.mydomain.com/freeseat -> http://login.mydomain.com/freeseat/
Why isn't the DirectorySlash Off working? I tried to put it in .htaccess, but that didn't help either.
Thanks a lot for your help,
David
First of all, you vHost won't work if you don't use the directive.
This works:
<VirtualHost *:80>
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
Allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
ErrorLog /var/www/login/error.log
</VirtualHost>
To answer you question: DirectorySlash works as expected.
Also see apache documentation for mod_dir, especially the red box called "Security Warning" of section "DirectorySlash".
Tip: always use not publicly accessible error logs to determine the root of evil. :)
Edit #1:
I think I might have misunderstood your question. You want to access /var/www/login/freeseat/index.php through http://login.mydomain.com/freeseat (w/o trailing slash).
Then, drop the freeseat folder and use index.php as var/www/login/freeseat.php and create a RewriteRule in the /var/www/login/.htaccess file (mod_rewrite must be active):
RewriteEngine On
RewriteRule ^freeseat$ freeseat.php
Also, drop
Options +Indexes
DirectoryIndex index.html index.php
from your vHost configuration. It isn't needed anymore.
I am trying rewrite my url by .htaccess file. I tried to find out solution before I ask but nothing to seem work. Could you please help out? Thank you.
Here is my .htaccess file (In the root folders):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteRule ^film/([^/]*)/([^/]*)\.html$ /html/movie/film.php?film=$1&genreID=$2 [L,QSA]
</IfModule>
I did set these lines (in etc/apache2/sites-available/ 000-default.conf):
DocumentRoot /var/www
<Directory "/var/www/html/movie">
AllowOverride All
</Directory>
I am using Linux mate Apache 2.4.7 and in error.log is nothing wrong.
Do you allow mod_rewrite in php.ini?
You have an .htaccess in the document root folder, which is /var/www according to 000-default.conf.
But you don't allow .htaccess files in /var/www. You allow .htaccess files in /var/www/html/movie, again according to 000-default.conf.
To allow .htaccess files in /var/www or /var/www/html, you must say so, e.g.
<Directory /var/www>
AllowOverride All
</Directory>
or
<Directory /var/www/html>
AllowOverride All
</Directory>
N.B. you don't need AllowOverride All, AllowOverride FileInfo is sufficient for using mod_rewrite, see AllowOverride
I have a working project with Symfony2.
One of my bundles works well by default but when I activate mod_rewrite I get
This script is only accessible from localhost
This happens only with the routes configured on this bundle, others work fine with mod_rewrite
Here is my vhost config
<VirtualHost *:80>
ServerName my_application.my_domain.net
ServerAdmin xxx#xxx.xxx
DocumentRoot "/var/www/my_application/web"
<Directory /var/www/my_application/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from All
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</Directory>
</VirtualHost>
Where can that come from ?
Update
Example
I'm getting the error when trying to access
my_application.my_domain.net/config/list/produit
If I disable mod_rewrite I can access
my_application.my_domain.net/app.php/config/list/produit
Are you hitting app_dev.php or config.php?
By default, both of those restrict connections from anywhere but localhost and display that exact same message.
Update
After the update, I think the problem may be because you have MultiViews enabled. MultiViews can try to load config.php even if it is just referenced as config. Try removing that and see if it helps improve anything.