Why does this config code not work in .htaccess? - apache

<VirtualHost *:80>
DocumentRoot /lf/main/com
ServerName 74.220.215.241/~laborfa2
ServerAlias 74.220.215.241/~laborfa2
RewriteEngine on
#RewriteLogLevel 2
#RewriteLog logs/rewrite.log
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule ^/(.*)(/?)$ /$1.php [L]
RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})(/?)$ /profile.php?fairid=$1$2 [L]
RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})/([a-z]*)(/?)$ /$3.php?fairid=$1$2 [L]
</VirtualHost>
It works fine on linux(htt.vhost) but when i paste it in .htaccess does not works.
So what do i need to change to make it work?

G'day,
As mentioned above, the context for the VirtualHost directive explicitly excludes its use in .htaccess files:
From the Apache 2.2 manual:
server config ... means that the directive may be used in the server configuration files (e.g., httpd.conf), but not within any or containers. It is not allowed in .htaccess files at all.
HTH
cheers,

You cannot setup a vitual host inside a virtual host.

The <VirtualHost> block that you are using can only be configured inside the httpd.conf file (main server config) and will not work inside .htaccess.
You can try moving the VirtualHost inside httpd.conf and just leving RewriteCond and RewriteRule inside the .htaccess file

The ServerName directive should contain a server name and not a URL. Setting it to a value of www.example.com:80 is valid, but www.example.com/~example is not. See the Apache mod_core documentation.
Other than that, even though the entry has been commented out, you can't use the RewriteLog directive in an .htaccess file. See the Apache mod_rewrite documentation.

You cannot use the RewriteLog directive in a .htaccess file. See the mod_rewrite documentation. You should also take a look in the error log when you run into such problems.

Related

Phabricator rewrite rules configuring issue

I'm having trouble installing phabricator. It actually seems like it would be a simple thing. I copied the example code exactly.
apache2.conf:
<VirtualHost *>
# Change this to the domain which points to your host.
ServerName localhost
# Change this to the path where you put 'phabricator' when you checked it
# out from GitHub when following the Installation Guide.
#
# Make sure you include "/webroot" at the end!
DocumentRoot /var/www/phabricator/webroot
RewriteEngine on
RewriteRule ^/rsrc/(.*) - [L,QSA]
RewriteRule ^/favicon.ico - [L,QSA]
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]
</VirtualHost>
<Directory "/var/www/phabricator/webroot">
Require all granted
</Directory>
Whenever I go to the server IP on a browser, it gives me this error:
Request parameter '__path__' is not set. Your rewrite rules are not configured correctly.
I found that this was part of the phabricator code:
if (!isset($_REQUEST['__path__'])) {
self::didFatal(
"Request parameter '__path__' is not set. Your rewrite rules ".
"are not configured correctly.");
}
Anyone have any idea how to get past this?
I had a similar issue with Phab and solved it with the following:
Place the Directory segment inside the VirtualHost segment.
Are you running any other Virtual Server in your system? If so, specify the port *:80 (Or try a different one Remember adding Listen 8081 before declaring the VirtualHost segment if you try another port)
And last replace the content of the Directory segment with this:
Order allow,deny
Allow from all
I was also facing same issue.
Just place
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]
and remove following line from your apache2.conf.
RewriteRule ^/rsrc/(.*) - [L,QSA]
RewriteRule ^/favicon.ico - [L,QSA]

how do I force all traffic to https?

I'm trying to use .htaccess to send all traffic to https. I'm using the example in this answer: Need to redirect all traffic to https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This isn't redirecting anything for me. I placed it in var/www but it doesn't seem to have any effect. What did I do wrong?
Try [R,L] at the end of the rewrite rule:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
R redirects to the provided link, and L breaks the flow if condition is met.
For reference you can see:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
If that's the only rules you have in your htaccess file and it's in your document root then you need to check a few things because the rules are fine.
Make sure mod_rewrite is loaded. There should be a line in your httpd.conf file that looks something like:
LoadModule rewrite_module modules/mod_rewrite.so
Make sure it's uncommented.
Make sure the directory that your htaccess file is in (should be your document root) is allowed to override server settings via htaccess. In your vhost or server config, there should be something along the lines of
<Directory "/var/www/">
AllowOverride All
... (some other stuff)
</Directory>
Make sure the AllowOverride is at least FileInfo
Make sure your document root is actually where your htaccess file is in. Your vhost config should have a line like:
DocumentRoot /var/www/
Make sure the document root is for the right vhost. If you have separate vhosts for SSL and non-SSL, make sure the htaccess file is in the document root for the non-SSL vhost.

mod_rewrite: redirect requests from localhost to remote server

I have a following scenario:
Remote server with some webapp running at http://remote/webapp
Local machine inside corporate network
Corporate proxy between them
Apache with mod_rewrite running on my local machine
I would like to have the mod_proxy rewrite every request like http://localhost/webapp?someparams into http://remote/webapp?someparams.
Currently I have the following httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
<Directory />
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Which results in mod_rewrite transforming http://localhost/webapp?someparams into http://remote/C:/Apache2.2/htdocs/webapp?someparams
How to configure mod_rewrite to handle it correctly?
Since it looks like you have access to vhost/server config, you should ditch mod_rewrite and just use mod_proxy:
ProxyPass /webapp http://remote/webapp
ProxyPassReverse /webapp http://remote/webapp
and get rid of the 2 mod_rewrite lines (which is redirecting, not proxying):
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Note that if you have cookies, you may need to reverse map their domains.using ProxyPassReverseCookieDomain.
Also:
The fact that windows absolute path appears in the URL is due to misconfiguration of the mod_rewrite and this is what I'm trying to avoid
This is not a misconfiguration with mod_rewrite. When you put rewrite rules inside a <Directory>, the filepath is used in the match instead of the URI-path. According to the mod_rewrite documentation
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Thank you Jon for inspiration, finally mod_proxy + mod_rewrite worked:
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/
I know that this is a simplified and coarse solution, but works for my purpose.

apache - RewriteEngine doesn't work outside <VirtualHost> Directive

I'm trying to configure apache to redirect the requests containing only hostname (domain) to another path.
I found the following directives useful, but as long as those are enclosed in VirtualHost, put in site_available folder (/etc/apache2/) and enabled as a separate site (with a2ensite command).
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
</VirtualHost>
Note that only Rewrite* directives don't work. The alias to my Python project works fine.
Morover, I found out that if I disable other virtual host(s) (000-default) with a2dissite command and put the directives mentioned above (without VirtualHost) to httpd.conf file everything (including the Rewrite* rules) work.
So, I am in doubt why I couldn't get this done in simple server config context, and what is the solution?
The reason why it appears that the Rewrite* directives do not work in the server configuration and need to be declared inside the VirtualHost is because (from the RewriteEngine documentation):
[...] rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
To change this behaviour, you can set RewriteOptions to something InheritDownBefore or similar. You then still need to have a RewriteEngine On on each VirtualHost but at least you won't need to copy the rewrite rules on each one.
So, taking your code example, this should work:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
</VirtualHost>

How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I do this?
I found this but my question is the same: how can I do this without resorting to .htaccess files and performing some other action for each virtual host?
OMGTIA!
Specify RewriteOptions InheritDown in the parent scope (such as httpd.conf) to get your rules applied in child Virtual Hosts without modifing them.
This will only work on Virtual Hosts where the RewriteEngine directive is set to on:
Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
(source)
Apache supports this since 2.4.8 (not available at the time of the original question).
From documentation for RewriteOptions:
InheritDown
If this option is enabled, all child configurations will inherit the configuration of the current configuration. It is equivalent to specifying RewriteOptions Inherit in all child configurations. See the Inherit option for more details on how the parent-child relationships are handled.
Available in Apache HTTP Server 2.4.8 and later.
InheritDownBefore
Like InheritDown above, but the rules from the current scope are applied before rules specified in any child's scope.
Available in Apache HTTP Server 2.4.8 and later.
IgnoreInherit
This option forces the current and child configurations to ignore all rules that would be inherited from a parent specifying InheritDown or InheritDownBefore.
Available in Apache HTTP Server 2.4.8 and later.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions)
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <VirtualHost> section:
RewriteEngine On
RewriteOptions Inherit
click http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html to find more information
Looks like the simplest possible solution is to add
RewriteOptions inherit
to each VirtualHost directive. This is at least a lot simpler than messing with .htaccess files. Apache is pretty clear on the fact that
by default, rewrite configurations are
not inherited. This means that you
need to have a RewriteEngine on
directive for each virtual host in
which you wish to use it.
(http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)
and apparently the way to change the default is via RewriteOptions in the child (vhost or director), so you have to do something in each child.
I've never tested it, so it might not work, but I would try adding an include directive in all of the virtual host blocks to a single file. You would have to change each virtual host configuration block once, but after that, you should have a central place from which to make changes. YMMV.
If you're only trying to rewrite something in the domain part of the name, e.g. to fix a common misspelling, you don't even need the 'inherit' option. I setup a no-name virtual host to catch all invalid host names and respell them correctly before redirecting them.
Since this uses redirects, the appropriate virtual host will be found after the rewrites have been applied.
Options +Indexes +FollowSymLinks
RewriteEngine on
# If it begins with only domain.com, prepend www and send to www.domain.com
RewriteCond %{HTTP_HOST} ^domain [NC]
RewriteRule ^(.*) http://www.domain.com$1 [L,R=301]
# Correct misspelling in the domain name, applies to any VirtualHost in the domain
# Requires a subdomain, i.e. (serviceXXX.)domain.com, or the prepended www. from above
RewriteCond %{HTTP_HOST} ^([^.]+\.)dommmmmain\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+\.)?domain.com(.*) http://$1domain.com$2 [L,R=301]
# No-name virtual host to catch all invalid hostnames and mod_rewrite and redirect them
<VirtualHost *>
RewriteEngine on
RewriteOptions inherit
</VirtualHost>
You may want to use InheritDownBefore to avoid having to add more junk to your vhosts.
An example of a global letsencrypt alias:
# letsencrypt
<IfModule alias_module>
Alias /.well-known/ /var/www/html/.well-known/
</IfModule>
<IfModule mod_rewrite.c>
# prevent vhost rewrites from killing the alias
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/\.well\-known
RewriteRule . - [L,PT]
</IfModule>
Then you can do this in each of your vhosts, with no other directives:
<VirtualHost *:80>
....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/.* /index.php [L,PT]
</IfModule>
</VirtualHost>
Thanks to everyone to answered above. It helped me find my answer.
Question has been answered already, I just wanted to add an example in case you are using Google Compute Engine. It says it requires Apache HTTP Server 2.4.8 BUT it works with Apache/2.4.25 (Debian). Even when I try to upgrade, I cannot go past Apache/2.4.25. It says this version is the latest version.
Here's an example of how to implement.
RewriteOptions InheritDown
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} !\.co$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.[^.]+$ [NC]
RewriteRule ^ https://%1.co%{REQUEST_URI} [L,NE,R=301]
<VirtualHost *:80>
RewriteEngine On
ServerAlias *.*
</VirtualHost>
ALSO OF NOTE (For Testing):
When you are testing your rewrite engine. It is really easy to get confused about if it is working or not because of cache and cookies. If you got it to work once on a browser, it will continue to work even if you delete the rewrite code. Testing rewrite is really annoying sometimes. You might think it works but then it stops or starts.
Best way to test rewrite code is to open an incognito tab in your browser, clear or cookies and cache. Open developer mode just in case. DO NOT JUST REFRESH. You need to click into the URL and refresh. Or open new tab. Or copy/paste URL into new window. If you use same window with refresh, it might be just redoing results from the past instead of renewing the new code.
I've always used a "catch-all" VHost for directives I wanted across the board, like......
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
ErrorLog "/var/log/apache2/error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName alloftherestoftheVHosts.com
DocumentRoot "/ServiceData/.........
............
And it's always seemed to work... error logs were getting combined properly, etc...... but it IS possible that this was the result of an earlier / conflicting / like-minded directive.
Personal note.. Whoever dreamed up the Apache configuration schema and syntax was a dingbat, or a group of dingbats, who spent too much time in their cave.... The whole thing should be exorcised and XMLized, or something! Although they are both wildly different... the Hello-Kitty setup process of Cherokee.. to the viciously succinct NGinx config.... are both so much more logical..