I made myself a dynamic vhost file for all directories in /home/lukasz/websites
<VirtualHost *:80>
ServerName vm01.dev
ServerAlias *.vm01.dev
SetEnv APPLICATION_ENV "lukasz"
VirtualDocumentRoot /home/lukasz/websites/%1/public
<directory /home/lukasz/websites/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</directory>
</VirtualHost>
It works well until very simple rule from mod_rewrite comes to play. If I add following code to the .htacesss
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
The apache server will be unable to respond. It will go into an infinite loop and will break out from the execution after 10 redirects. This problem is tightly related to VirtualDocumentRoot I'm using. How should I modify my config to keep it dynamic and stop it breaking with about htaccess?
I'm guessing you want to make index.php the default page, when the requested resource doesn't exist, you didn't say.
If this is the case then adding an ErrorDocument entry to your virtualHost will suffice e.g.
ErrorDocument 404 /index.php
If you are going to use mod_rewrite, it's far more efficient to place the rules in the Virtualhost definition than in a .htaccess file. As the httpd.conf is read once at start-up and all rule compiled, as opposed to the .htaccess file being read and all rules parsed for each request. Anyway the following should stop the loop:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
Assuming you have an: /index.php
FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.
Related
I Have the next configuration
<VirtualHost *:443>
ServerName projectsite.com
ServerAlias projectsite.com
DocumentRoot /var/www/project
<Directory />
RewriteEngine On
Options +FollowSymlinks
AllowOverride All
Require all grant
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>
</VirtualHost>
The project manage all urls from the client, so, I only need to return the index.html file and I render the modules with javascript. It works ok but If the url matches with a folder then I am getting a 403 error "Forbidden". Instead of this, I need return the root index.html file either is the url matches with a directory name or not.
¿How can I do it ? I tried to find on internet but I didn't found something similar.
UPDATED:
how Dusan Bajic told me, I removed that line RewriteCond %{REQUEST_FILENAME} !-d and it resolved the first problem, but now the Rule is adding a slash at the final of the url,
If I have "site.com/reports" it changes to "site.com/reports/". I need to mantain the url without this slash. I think the problem is with the
next Rule:
RewriteRule ^/?.+$ / [L]
But i don't know the correct way to make it
How #dusanBajic commented I had a rule that prevented what I need.
The rule is :
RewriteCond %{REQUEST_FILENAME} !-d
This rule indicates that the rewrite rule only apply if the folder does not exists. So what I did was Remove that instruction and add the next rule DirectorySlash Off to prevent apache puts a slash at the final of the url.
The code left like this:
<Directory />
RewriteEngine On
Options +FollowSymlinks
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>
I have a very straightforward url friendly htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)/?$ index.php?id=$1
So domain.com/something is mapped to domain.com/index.php?id=something.
Every URL can have an additional parameter, so for example domain.com/something can be in the form of domain.com/something?custom_value
Now I need to address this particular situation: If a particular ID is matched, then a custom value must be forced. So, if I go to domain.com/somethingelse I want to be redirected to domain.com/somethingelse?custom_value
I've tried different rules with no luck. This was my last attempt but I get a message that the server is making a rediretion that will never be completed.
RedirectMatch 301 ^/(somethingelse)/?$ /somethingelse?custom_value
Another attempt was this, resulting in a 500 Internal Server Error
RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
complete htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
RewriteRule ^(\w+)/?$ index.php?id=$1
EDIT 1:
Well, apparently this rule works (not always) on local machine, but no online server
RedirectMatch 301 ^somethingelse$ somethingelse?custom_value
EDIT 2:
I've also tried (also works on local machine, but no online server)
RewriteRule ^/somethingelse$ /somethingelse?test [L,R=301]
EDIT 3
These are my virtualhosts:
local virtual host
<VirtualHost vhostname>
DocumentRoot /var/www/path/to/project
<Directory /var/www/path/to/project>
AllowOverride All
</Directory>
</VirtualHost>
server virtual host
<VirtualHost *:80>
ServerName subdomain.domain.com
DocumentRoot /var/www/path/to/project
<Directory /var/www/path/to/project
AllowOverride All
</Directory>
</VirtualHost>
You may use these rules in your site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^somethingelse/?$ %{REQUEST_URI}?custom_value [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/?$ index.php?id=$1 [L,QSA]
Make sure to test this in a new browser or test it after completely clearing your browser cache.
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]
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.
I have a VHOST with the following rule
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]
Pretty much just makes every URL go through an advanced routing system
However, its becoming a conflict when trying to set up my news system using WordPress.
All I need help with is creating a new rewrite rule to put all through the router with the exception of one directory, for example named "wordpress."
Its all on my local machine, but here is the entire VHOST config
<VirtualHost *:80>
DocumentRoot "/Users/tyler/Documents/mysite"
ServerName mysite.local
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]
<Directory "/Users/tyler/Documents/mysite">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
All I need help with is creating a new rewrite rule to put all through
the router with the exception of one directory, for example named
"wordpress."
Based on that criteria, you should add the following rule to the mix:
RewriteCond %{REQUEST_URI} !^/wordpress
Which will make the whole ruleset now be as follows:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]