So I have one website, for example http://example.com/ and another one http://example.myeshop.com/. I want to make accessible URL http://example.com/eshop to be same as http://example.myeshop.com/, but no redirects. Also when someone use link like http://example.com/eshop/contact it would have same effect as http://example.myeshop.com/contact and so on. This is my .htaccess file:
IndexIgnore */*
RewriteEngine On
RewriteRule /eshop/(.*) http://example.myeshop.com/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/xhtml text/html text/plain text/xml text/javascript application/x-javascript text/css
</IfModule>
FileETag none
But doesn't seem to be working. Can you help me how to achieve such a thing?
What you are looking for is mod-proxy. Enable proxy module on your server and then you can use this :
RewriteEngine on
RewriteRule ^eshop/(.*)$ http://example.myeshop.com/$1 [P]
This will internally forword all requests of example.com to example.myeshop.com . You will get an internal server error if the proxy module isnt enabled.
If you have access to httpd.conf you can use below rules in your httpd.conf file where section for mod_proxy is mentioned or might be you have to put in extra/httpd-ajp.conf.
ProxyPass /eshop/ http://example.myeshop.com/
ProxyPassReverse /eshop/ http://example.myeshop.com/
These two lines should be uncommented in your httpd.conf file.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Related
I'm currently hosting a PHP application on Apache. This application is behind a CloudFront distribution that sends all requests with cms/* to my application.
I used the following .htaccess file and hoped to make things work:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
</IfModule>
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
I get an Internal Server Error and in the Apache Error logs this can be found:
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error.
What am I doing wrong?
My CMS is running in the document root of the Apache server.
If the CMS is in the document root then you should remove the RewriteBase /cms directive entirely.
The presence of RewriteBase /cms results in the request being internally rewritten to /cms/index.php (not /index.php) and if this file does not exist then you will naturally get a rewrite-loop (the preceding condition checks that the request does not map to a file or directory).
In other words (with some additional improvements):
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
DirectoryIndex is likely already set in the server config, although it is necessary here.
The first RewriteRule directive is an optimization to prevent unnecessary filesystem checks when the request is rewritten.
There is no need to traverse and capture the entire URL-path in the last RewriteRule directive (ie. ^(.*)). The regex . (a single dot) is sufficient, and more efficient.
No need for the <IfModule mod_rewrite.c> wrapper, unless these directives are optional.
I solved it the following way:
I put the application on the Apache server in a cms folder and kept the current .htaccess as is. It might also work with keeping it in the Apache root folder and setting the RewriteBase to something like ../ if that works with Apache but I'm happy with the current solution.
I have a LAMP server, but I do more website coding and design than Apache configuration. I hope my question will be clear because I don't understand the syntax of .htaccess files very well, and the code I have is mostly derived from tutorials on the web.
My website uses "friendly URLs", and to enable that function, I have the following code in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
However, I've created a subdirectory with a whole different set of HTML under a subdirectory that I want to be able to access by going to a URL like this: http://www.example.com/subdirectory.
What is happening is that even if I go directly to http://www.example.com/subdirectory/index.html, Apache still redirects to index.php in the root.
Is there a way I can modify my .htaccess file so that my subdirectory will be exempted from the redirection?
UPDATE:
After some experimenting, I've determined that it is exactly this line that is causing the problem (which may be obvious to people more skilled than I am with .htaccess files):
RewriteRule ^(.*) index.php
If I comment that line out, everything in the subdirectory works fine (the rest of the site, of course, breaks). If I leave it in, I get the problem described above.
As suggested in an answer below, I tried changing the line to:
RewriteRule !^subdirectory index.php
But that didn't fix the problem. I also tried making a .htaccess file in the subdirectory with these contents:
RewriteEngine On
Unfortunately, that did nothing.
My limited understanding of .htaccess syntax says that the line I already have should leave the contents of my subdirectory alone, but it is acting far more aggressively than intended.
Is there a way I can diagnose and fix this line so that it does not act upon the contents of my subdirectory?
UPDATE 2:
After some experimentation, I found that the .htaccess file in the subdirectory is not being read. If I put gibberish text in it, I don't get any errors, which seems to indicate it's not even being accessed.
I looked for reasons why my .htaccess file might not be being read, and I found that in my .conf file for the site, I need to have this code:
AllowOverride All
I have that directive in there, but the .htaccess in the subdirectory is still not being read.
What to I do to ensure the .htaccess file in the subdirectory is being read?
Contents of my root .htaccess file:
# BEGIN Compress text files
<IfModule mod_deflate.c>
<FilesMatch "\.(css|js|x?html?|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# END Compress text files
# BEGIN Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 2592000 seconds"
ExpiresByType application/javascript "access plus 2592000 seconds"
ExpiresByType application/x-javascript "access plus 2592000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
# END Expire headers
# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
<FilesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>
<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
<FilesMatch "\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
</IfModule>
# END Cache-Control Headers
# BEGIN Turn ETags Off
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
# END Turn ETags Off
# BEGIN Remove Last-Modified Header
<IfModule mod_headers.c>
Header unset Last-Modified
</IfModule>
# END Remove Last-Modified Header
# Make all requests pass through index.php to enable "friendly URLs"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
# Turn off 'magic quotes'
php_value magic_quotes_gpc off
# Do not allow Perl script hackers, as they are probably just feeding useless Adsense "directory" sites
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* - [F,L]
# Filter for most common exploits
RewriteCond %{HTTP_USER_AGENT} libwww-perl [OR]
RewriteCond %{QUERY_STRING} tool25 [OR]
RewriteCond %{QUERY_STRING} cmd.txt [OR]
RewriteCond %{QUERY_STRING} cmd.gif [OR]
RewriteCond %{QUERY_STRING} r57shell [OR]
RewriteCond %{QUERY_STRING} c99 [OR]
# deny most common except .php
<FilesMatch "\.(inc|tpl|h|ihtml|sql|ini|conf|class|bin|spd|theme|module)$">
deny from all
</FilesMatch>
# Disable .htaccess viewing from browser
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
This code is in my /etc/apche2/apache2.conf:
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess
... and here is the contents of the conf file specific to the site in question:
<VirtualHost *:80>
ServerName www.local_example.com
ServerAlias local_example.com
ServerAdmin serveradmin#gmail.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com/>
Options Indexes FollowSymLinks MultiViews
# pcw AllowOverride None
AllowOverride All
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
</Directory>
ErrorLog /home/admin/Apache_Logs/local_example.com_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /home/admin/Apache_Logs/local_example.com_access.log combined
ServerSignature On
</VirtualHost>
If /subdirectory is a physical directory on the filesystem and /subdirectory/index.html is an actual file then your current directives already include the necessary exception... the URL should not be rewritten if the requested URL maps to a physical directory or file.
However, to explicitly include an exception for the /subdirectory then you could do something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^subdirectory index.php
Only URLs that do not start /subdirectory will be processed. (Not that the directory-prefix is removed when matching the URL-path with the RewriteRule pattern, so it should be subdirectory here, not /subdirectory.)
Alternatively, you can create an additional .htaccess at /subdirectory/.htaccess and simply enable the rewrite engine:
# /subdirectory/.htaccess
RewriteEngine On
mod_rewrite directives are not inherited by default, so this should completely override the mod_rewrite directives in the parent .htaccess file. (Note that other directives from different modules might still be processed.)
UPDATE: I cannot see anything in your .htaccess file that would directly cause these symptoms, however, the following does need fixing/tidying...
# Do not allow Perl script hackers, as they are probably just feeding useless Adsense "directory" sites
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* - [F,L]
# Filter for most common exploits
RewriteCond %{HTTP_USER_AGENT} libwww-perl [OR]
RewriteCond %{QUERY_STRING} tool25 [OR]
RewriteCond %{QUERY_STRING} cmd.txt [OR]
RewriteCond %{QUERY_STRING} cmd.gif [OR]
RewriteCond %{QUERY_STRING} r57shell [OR]
RewriteCond %{QUERY_STRING} c99 [OR]
The 2nd code block above ("Filter for most common exploits") is incomplete and should not have a trailing OR flag (that would potentially block everyone!). These two code blocks also repeat the same code and should be combined. The RewriteBase directive is also redundant and can be removed. The above should be rewritten as:
# Do not allow Perl script hackers, as they are probably just feeding useless Adsense "directory" sites
# and Filter for most common exploits
RewriteCond %{HTTP_USER_AGENT} libwww-perl [OR]
RewriteCond %{QUERY_STRING} tool25 [OR]
RewriteCond %{QUERY_STRING} cmd.txt [OR]
RewriteCond %{QUERY_STRING} cmd.gif [OR]
RewriteCond %{QUERY_STRING} r57shell [OR]
RewriteCond %{QUERY_STRING} c99
RewriteRule .* - [F]
The F flag implies L, so the L flag is not required here.
UPDATE#2: Again, I cannot see anything in your VirtualHost container that would cause these problems (although, to be honest, I'm struggling to even imagine what could be causing this behaviour).
However, the following line is suspicious:
Options Indexes FollowSymLinks MultiViews
Why do you need MultiViews? Also, do you need Indexes? Most sites will disable these options as they can expose your file structure and result in unexpected behaviour (unless that behaviour is intentional of course). However, FollowSymLinks is required for mod_rewrite.
If these options are not required then try removing them:
Options FollowSymLinks
Incidentally, these options can also be disabled in .htaccess with the following directive (note that here the options are preceded with -):
Options -Indexes -MultiViews
My shared host offers shared SSl certificate like the following:
https://host###.HostMonster.com/~username
Where host### is the hosting server for my account there.
I use CakePHP app on the root of public_html where I have public_html/webroot path and I use the following .htaccess settings on public_html:
# Turn off the ETags
Header unset ETag
FileETag None
# Turn off the Last Modified header except for html docs
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|mp3)$">
Header unset Last-Modified
</FilesMatch>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|mp3)$">
Header set Expires "Thu, 15 Jan 2015 20:00:00 GMT"
</FilesMatch>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|mp3)$">
Header set Cache-Control "max-age=31449600"
</FilesMatch>
# Use PHP5.3 as default
AddHandler application/x-httpd-php54 .php
Redirect /Hosting/Q/ http://sub.mydomain.net/
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteCond %{HTTPS} =on
#RewriteBase /~twoindex/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
AddType audio/mpeg mp3
AddType text/xml xml
When I try to access https://host15.HostMonster.com/~myuser where host15 is my host server, I got 404 error.
I tried the following:
Creating a simple html file and placing it on public_html and then accessing it using https://host15.HostMonster.com/~myuser/simple.html. It also returns 404 error.
I tried to comment out the two lines after RewriteEngine on but no success too.
Removing the contents of the .htaccess file i.e making it empty, allowed me to access simple.html via https successfully.
The last try, is not a real option for me because it will destroy my CakePHP application. However, it approved that it is a problem related with rewriting rules in the .htaccess. So, I need to know how to modify the .htaccess to allow accessing my CakePHP application while allowing accessing the simple.html or better a sub directory on public_html
Update
I tried the following rule, but it ended with the error This webpage has a redirect loop when I try to access https://host15.HostMonster.com/~myuser/folder and it keeps everything fine for the CakePHP application.:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^host15\.HostMonster\.com [NC]
RewriteRule ^(.*)$ /~myuser/$1 [R,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
I have a strange problem where I have two separate urls pointing to the same Rails application
staging.abcxyz.com
staging.abcttt.com
My setup runs apache with passenger. The important thing to note here is that both urls point to the same Rails application in the Document Root directory and are just served by a different urls internally.
Problem
When i try to access staging.abcxyz.com it works and the application is rendered and everything works as expected.
When i try to access staging.abcttt.com apache returns a 403 forbidden.
Apache Config
This is apache configuration of the url that doesn't work for me. Its exactly the same as the one that works expect for changes in the urls.
<VirtualHost *:80>
ServerName staging.abcttt.com
ServerAlias www.staging.abcttt.com
DocumentRoot /srv/abcxyz/current/public/
<Directory /srv/abcxyz/current/public/>
Order allow,deny
Allow from all
Options FollowSymLinks
AllowOverride None
</Directory>
RewriteEngine on
RewriteRule ^/$ /s/home [P]
RailsAutoDetect On
RackEnv staging
RailsEnv staging
RailsSpawnMethod smart
## PassengerAppGroupName
#
# By default, Passenger groups applcations by the the path they are served out of,
# ie /srv/yourapp/current.
#
# At times, it may be useful be serving the same app from multiple vhosts, but have
# them be have different workers. For example, you may have a /ping URL that needs to
# respond quickly, without being affected by the rest of the app. In this case, you can:
#
# * create a new vhost pointing at the same app
# * set PassengerAppGroupName to ping
# * configure a proxy to forward /ping to the new vhost
PassengerAppGroupName abcxyz
# Deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript application/javascript application/json
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
RequestHeader set X-Request-Start "%t"
RewriteEngine On
# Check for maintenance file and redirect all requests
ErrorDocument 503 /system/maintenance.html
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [R=503,L]
# Rewrite index to check for static
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}/index.html -f
RewriteRule ^/?$ /index.html [QSA,L]
# Rewrite to check for Rails non-html cached pages (i.e. xml, json, atom, etc)
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^(.*)$ $1 [QSA,L]
# Rewrite to check for Rails cached html page
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ $1.html [QSA,L]
</VirtualHost>
Questions
I don't understand why this would be a permissions issue because staging.abcxyz.com can already access the folder. Am i missing something here since both are served from the same directory
Could it be something to do the with the PassengerAppGroupName - but i'm not particularly concerned about having a separate worker send respond to a particular request
I'd really appreciate any help on this. Thanks.
Update
A thing that i notice is if I use the R [redirect] flag instead of the P [proxy] the application works and redirects to the right url but i want to be an internal redirect which does not reflect on the browser.
I have a problem with apache2 settings (Ubuntu system).
I would like to run symfony project on my localhost but instead of serving .phtml files, browser is trying to download files.
alt text http://www.freeimagehosting.net/uploads/ba9be708fc.gif
this is my file .host:
127.0.0.3 test
this is apache2/sites-available/default file
< VirtualHost 127.0.0.3:80>
ServerName test DocumentRoot
"/home/m/Pr/workspace/php/test/web"
DirectoryIndex frontend_dev.php
< Directory
"/home/m/Pr/workspace/php/test/web">
AllowOverride All
Allow from All
Alias /sf
/home/m/Pr/workspace/php/test/lib/vendor/symfony/data/web/sf
< Directory
"/home/m/Pr/workspace/php/test/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All </Directory>
and this is .htaccess in /test
RewriteEngine On RewriteRule ^(.*)$
/web/$1 Options +FollowSymLinks
+ExecCGI AddHandler application/x-httpd-php5 .php .phtml
and this is .htaccess in /test/web
Options +FollowSymLinks +ExecCGI
RewriteEngine On
# uncomment the following line, if
you are having trouble # getting
no_script_name to work RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} ..+$ #RewriteCond %{REQUEST_URI} !.html$
#RewriteRule .* - [L]
# we check if the .html version is
here (caching) RewriteRule ^$
index.html [QSA] RewriteRule
^([^.]+)$ $1.html [QSA] RewriteCond
%{REQUEST_FILENAME} !-f
# no, so we redirect to our front
web controller RewriteRule ^(.*)$
index.php [QSA,L]
Another problem is I think apache don't read .htaccess files.
What am i doing wrong? Maybe I forgot about something? Please, help me becouse i have no idea.
You need to declare the AddType directive in your Apache config - I'm not sure adding it to .htaccess will work.
Add this line to either /etc/apache2/mods.enabled/php.conf or /etc/apache2/httpd.conf:
AddHandler application/x-httpd-php5 .php .phtml
Restart Apache and retry.
Check the Content-Type headers the server is sending for the .phtml files - chances are it's something the browser doesn't recognise.
yes I don't remember Apache to be prepared for serving phtml files out-of-the-box.
I think you need to assert Content-Type is defined to text/html for the browser to render the file.