Incomplete htaccess for subdomain - .htaccess

I'm not sure if it can be done and I've looked high and low. It's possible I'm just not constructing the search query correctly to yield the results I need.
I have 2 domains with differing content on a host (one added onto the other). When I added the second domain on, all 3 of these scenarios would resolve:
(1) domain2.domain1.com
(2) domain1.com/domain2/
(3) www.domain2.domain1.com
(4) www.domain1.com/domain2/
(5) domain2.com
I only want to show content for https://domain1.com or https://domain2.com -but-
https://domain1.com/domain2/ and https://www.domain1.com/domain2/ should resolve to https://domain2.com
The following changes to the .htaccess took care of scenario (1) & (3) but I cannot figure out how to handle scenarios (2) & (4):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2.domain1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.com [NC]
RewriteRule ^(.*)$ https://domain2.com/$1 [L,R=301,NC]
Any assistance would be greatly appreciated.

You need to create four virtual hosts, < VirtualHost >, for the same.
For domain1.com, you need to create a virtual host that redirect the http to https and another one that will take care of https connections.
Similarly for the domain2.com
ServerName and ServerAlias directives will help you in your problem (Name based virtual host).
More can be read for the same at link
Now your file will contain the below rules
<VirtualHost 192.168.23.34:80>
ServerName domain1.com
ServerAlias www.domain1.com
Redirect permanent / https://domain1.com/
</VirtualHost>
<VirtualHost 192.168.23.34:80>
ServerName domain2.com
ServerAlias www.domain2.com
Redirect permanent / https://domain2.com/
</VirtualHost>
<VirtualHost 192.168.23.34:443>
ServerName domain1.com
ServerAlias www.domain1.com
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/domain2
RewriteRule (.*) https://domain2.com/ [L,R=301,NC]
// ssl certificate setting for the same
</VirtualHost>
<VirtualHost 192.168.23.34:443>
ServerName domain2.com
ServerAlias www.domain2.com
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/domain1
RewriteRule (.*) https://domain1.com/ [L,R=301,NC]
// ssl certificate setting for the same
</VirtualHost>
Please make sure to restart the apache.
After this rule
www.domain1.com will redirect to https://domain1.com
domain1.com will redirect to https://domain1.com
www.domain2.com will redirect to https://domain2.com
domain2.com will redirect to https://domain2.com
https://domain1.com/domain2 will redirect to https://domain2.com
https://domain2.com/domain1 will redirect to https://domain1.com
https://www.domain1.com/domain2 will redirect to https://domain2.com
https://www.domain2.com/domain1 will redirect to https://domain1.com

Related

Rewrite rule not working with apache2, lucee tomcat

I have configured server with lucee tomcat and apache2 for virtual host on ubuntu. I have enabled rewrite rule and my virtual host is as followes.
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias example.com www.example.com
DocumentRoot /var/www/html/example.com/
<Directory /var/www/html/example.com/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error_main_example.log
CustomLog ${APACHE_LOG_DIR}/access_main_example.log combined
DirectoryIndex index.cfm
</VirtualHost>
redirect from htaccess file is working good but rewrite rule is not working. Here is the htaccess file that i am trying.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)/abc/([0-9]+)$ /test-404.cfm [L]
Here are the example URLs:
https://www.example.com/example.cfm/abc/2
https://www.example.com/example.cfm/abc/8
https://www.example.com/example.cfm/abc/15
It is showing me tomcat 404 error that
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/example.cfm/abc/2] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.35
Can any body help me about this issue. By the way, site is configured using classic load balancer on AWS.
I'm posting a working solution here also, because you didn't answer to our posts of this cross post at the Lucee forum. This solution might also help others with the same problem.
The issue is that mod_proxy will always preced urlrewrite, unless you invoke mod_proxy with a special urlrewrite rule. To make urlrewrite work and use mod_proxy, you need to flag the rewrite rule with [P] (for proxy). Here is a working example that should work for you:
Step: Set everything in apache2.conf in mod_proxy.c as comment, just leaving ProxyPreserveHost and ProxyPassReverse, like so:
<IfModule mod_proxy.c>
ProxyPreserveHost On
#ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ http://127.0.0.1:8888/$1$2
#ProxyPassMatch ^/(.+\.cfml)(/.*)?$ http://127.0.0.1:8888/$1$2
# optional mappings
#ProxyPassMatch ^/flex2gateway/(.*)$ http://127.0.0.1:8888/flex2gateway/$1
#ProxyPassMatch ^/messagebroker/(.*)$ http://127.0.0.1:8888/messagebroker/$1
#ProxyPassMatch ^/flashservices/gateway(.*)$ http://127.0.0.1:8888/flashservices/gateway$1
#ProxyPassMatch ^/openamf/gateway/(.*)$ http://127.0.0.1:8888/openamf/gateway/$1
#ProxyPassMatch ^/rest/(.*)$ http://127.0.0.1:8888/rest/$1
ProxyPassReverse / http://127.0.0.1:8888/
</IfModule>
Step: In your virtual host configuration set the following rewrite rules ( that may work also in your .htaccess, but I’m not sure).
...
<Directory /var/www/html/example.com/>
...
...
RewriteEngine On
RewriteBase /
# Catch non-existing files/directories and pass them via proxy to a 404 cfml errorpage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.*)$" "http://127.0.0.1:8888/my-404.cfm" [P]
# Catch https://www.example.com/example.cfm/abc/2
# Catch https://www.example.com/example.cfm/abc/7 etc.
RewriteRule "^example.cfm(/abc/[0-9]+)$" "http://127.0.0.1:8888/my-404.cfm" [P]
# Pass request for cfm/cfc files to proxy with mod_rewrite rule
RewriteRule "^(.+\.cf[cm])(/.*)?$" "http://127.0.0.1:8888/$1$2" [P]
...
</Directory>
I’ve tested it and the solution above works fine.

.htaccess rewrite subdomain keeps original url

I am writing a .htaccess rule that sends a subdomain request to a specific login page file. My current rule is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.name.com$
RewriteRule ^ https://name.com/app/login [R=302,L,NE]
This works as I would expect. I now want to build on this so as to keep the original url displayed. The RewriteRule would continue to redirect to https://name.com/app/login but still display https://subdomain.name.com as the URL within the web browser.
Is this possible within .htacess file? I cannot find a solution.
You could use:
RewriteCond %{HTTP_HOST} ^subdomain.name.com$
RewriteRule ^ https://name.com/app/login [P]
For this to work, you need to
1) enable apache proxy module
2) enable SSLProxyEngine in the VirtualHost definition:
<VirtualHost subdomain.name.com:80>
...
SSLProxyEngine on
</VirtualHost>
<VirtualHost subdomain.name.com:443>
...
SSLProxyEngine on
</VirtualHost>
3) If the certificate is not "trusted", e.g. is self signed, you could disable the proxy `s verification by adding:
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
to the virtualHost definitions above.
References
Redirect website but keep the original url in the address bar
Disable proxy ssl verification

godaddy subfolder domain htaccess redirect without changing browser url

Godaddy Deluxe Hosting with Linux
Main domain: www.domain1.com & 2 other hosted domains (www.domain2.com and www.domain3.com) are in subfolders
The contents of www.domain2.com .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ https://www.domain2.com/shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This will direct www.domain2.com/shop/tackle/1/0/
TO
www.domain2.com/shop/products.php?subcat_id=1&subcat2_id=0
but changes the url. I do understand that is because I'm includeing the https://www.domain2.com in the rewrite. However I try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
I get a 404 error. I does indicate that URL /domain2/shop/tackle/1/0/ can't be located.
Obviously the rewrite is including the domain2 folder name. I'm trying to keep the requested URL www.domain2.com/shop/tackle/1/0/ in the URL.
What I am missing?
If you have each domain pointing to correct DocumentRoot. as example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain1.com
DocumentRoot "/var/www"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
DocumentRoot "/var/www/domain2"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain3.com
DocumentRoot "/var/www/domain3"
</VirtualHost>
Then I don't think how the /domain2/ would be added onto relative request. But, if it is, then you should try RewriteBase directive to help with relative substitution is built correct. Like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This link has more information:
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritebase

Drupal 7 setting different Hosts for different request headers

I have a Drupal 7 website, some resources are being requests with the Host set to have the www prefix while others are not.
https://www.example.com when request header is 301 Moved Permanently
https://example.com when request header is 304 Not Modified
Changing the base_url does not appear to modify the behaviour.
Is there anyway I can make drupal set the Host as https://example.com for all the request headers?
There's an Apache wiki page, which shows a solution to your requirement. The first solution uses a virtual host setup and the Redirect directive
# Redirect every request to example.com
<VirtualHost *:80>
ServerName www.example.net
ServerAlias www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
# Define virtual host for example.com
<VirtualHost *:80>
ServerName example.com
DocumentRoot /usr/local/apache/htdocs
</VirtualHost>
And the second setup uses a mod_rewrite redirect
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R]
You can choose whichever fits your environment best.

.htaccess subdomain redirect problem

I have a website let's say www.example.com and I have a subdomain something.example.com
Both main domain and sub domain are pointing to directory "public_html"
I have .htaccess on root which redirects any URL without www to www.
For e.g. if user enters example.com then he will be redirected to www.example.com
If user enters example.com/mypage.html then he will be redirected to www.example.com/mypage.html
Now the problem is this is also affecting my subdomain. Because if someone enters something.example.com/visit.html then he is redirect to www.example.com/visit.html
I don't want this! If user enters subdomain then I don't want to redirected to www domain. This is what I have in my .htacces file
RewriteCond %{HTTP_HOST} !^www.stackoverflow.com$ [NC]
RewriteRule ^(.*)$ http://www.stackoverflow.com/$1 [R=301,L]
Can you please tell me what should I do to solve above problem?
Thanks.
Do you have access to your webserver configuration? This is better done by configuring the webserver.
On apache you would configure one virtual domain like this:
<VirtualHost *:80>
ServerName somedomainiwanttoredirect.com
ServerAlias maybe.somemoredomainstoredirect.com
ServerAlias orsubdomains.toredirect.com
RewriteEngine On
RewriteRule ^(.*)$ http://www.target.com/$1 [R=301,L]
</VirtualHost>
and on your real configuration, the www.target.com you add your subdomains that you do not want to be redirected:
ServerAlias subdomain.target.com

Resources