mod_rewrite with two servers and three subdomains? - apache

I've been looking around (both here and the googles), and I can't find an answer that actually works for me. Basically, I have two physical servers (a dev server and a production server), and I want to be able to use one .htaccess file in my git repo.
Normally this wouldn't be difficult, but I have two domains, example.com and longexample.com. I set up longexample.com and dev.longexample.com's CNAMEs to example.com (and dev.example.com) at my DNS host, and set the apache ServerName/ServerAlias on both servers:
dev server:
ServerName dev.example.com
ServerAlias dev.longexample.com
prod server:
ServerName example.com
ServerAlias longexample.com
ServerAlias www.example.com
ServerAlias www.longexample.com
Here's my .htaccess file:
Options +FollowSymlinks +Indexes
RewriteEngine on
# RewriteCond %{HTTP_HOST} ^www.longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [R,N]
RewriteCond %{HTTP_HOST} ^dev\.longexample\.com$ [NC]
RewriteRule ^(.*)$ http://dev.example.com/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R,NC,L]
Supposedly the first two blocks should rewrite prod and dev to the proper urls (I'll make them 301s when everything works), but it doesn't seem to be working. Like it is now, it does nothing. If I set the RewriteRule to [N] instead of [L], it gives me an infinite loop.
The first block is commented out because if I get it working on the dev server, the solution for prod should be readily apparent. The last block makes a request to example.com/about.html redirect to example.com/about (while still using the /about.html file. That part works fine.
Is there some issue with using two domains? I've also tried using %{SERVER_NAME} instead of %{HTTP_HOST}, but nothing changes.

Aha, figured it out! It seems having a chain of [OR]'s messes it up. Here's what works:
Options +FollowSymlinks +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoshokatana\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.yosho\.me$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^dev\.yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://dev.yosho.me/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R=301,NC,L]

Related

Redirect http to https using Symfony

I'm sorry to post a new question for something that has been asked many times but none of the answers I found worked for me.
I have a site in Symfony and I want it to use HTTPS and not HTTP, so I want to redirect the URLs.
For this, I have a .htaccess file in the public:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
And another in the racine, I've tried multiple things but none worked, here are some of my tests:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Or
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]
or
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://myurl [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?10.16.10.164)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ web/$1 [QSA,L]
Unfortunately, I'm not sure to understand right all of these and this is maybe why I'm not able to make it work.
There is other information you will maybe need:
My apache conf has this:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}:8000%{REQUEST_URI}"
</Location>
</VirtualHost>
or
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https:/myurl/$1 [NE,L,R=301
</VirtualHost>
I have this in my security.yaml:
access_control:
- { path: ^/, requires_channel: https}
In my framework.yaml:
trusted_proxies: '192.0.0.1,10.0.0.0/8, myurl'
# trust *all* "X-Forwarded-*" headers
trusted_headers: ['x-forwarded-for', 'x-forwarded-host', 'x-
forwarded-proto', 'x-forwarded-port', 'x-forwarded-prefix']
# or, if your proxy instead uses the "Forwarded" header
trusted_headers: ['forwarded']
I also created a certificate but I read somewhere that it's not a good way to do it with Symfony so I'm not using it. I did this because my error is: SSL_ERROR_RX_RECORD_TOO_LONG
And what I want is to redirect all HTTP to HTTPS and the:8000 to be automatically added.
Any ideas, please?

Apache redirect domain without www to the same with www except one page

I'm new in apache redirect.
I want to redirect example.org (without www) to www.example.org except one page
so I did this :
<VirtualHost *:8001>
ServerName example.org
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.org$
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
</VirtualHost>
and it doesn't work
Have any idea ?
thank you
Use this in your .htaccess instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^\/(example)
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
The first condition checks to see if www is not present. The second condition makes sure that it is not the directory example. If both of these conditions are met, then it will force www.
Make sure you clear your cache before testing this.
EDIT:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/robots.txt
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

https htaccess exclude some url

I know this question has been asked a lot, but i try a lot of answers and didnt succeed.
i try those answers:
.htaccess ssl & non-ssl redirects
Remove SSL integration from a specific folder using htaccess
and a few others that i find on google.
i only have ssl on 3 pages of the site and i want to make a general rule, that the other pages should be redirect to http when they are on https. im like Jon Snow i know nothing about apache and htaccess.
so this what i try so far:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^\/(page1|page2|page3)+ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
and this:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/page1/$
RewriteCond %{REQUEST_URI} !^/page2/$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
the part of send to http works but the exception dont or they send all to http or redirect to the index.
to clarify i didnt put both codes at same time.
other rules on:
RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L]
RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php
full url as requested:
https://www.example.com.br/area-restrita/
it can have more information after the 'area-restrita' part
Try this code:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(area-restrita|page2) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{HTTP_HOST} !^www\.example\.com\$
RewriteRule (.*) http://www.example.com.br/$1 [R=301,L,NE]
RewriteRule !\.(js|txt|ico|gif|GIF|jpg|JPG|png|PNG|css|swf|pdf|xml|XML|eot|EOT|ttf|TTF|woff|WOFF)$ index.php [L,NC]
if you want to redirect the whole site to https except for some particular directories / URLs and in these particular cases you still want them served via http, you can use this technique in virtual.conf; otherwise when the RewriteCond match, the particular pages will always receive the error "404 page not found"
<VirtualHost *:80>
ServerName www.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/app1/.*$ [NC]
RewriteCond %{REQUEST_URI} !^/app2/.*$ [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !=apps.example.com [NC]
RewriteCond %{REQUEST_URI} ^/app1/.*$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/app2/.*$ [NC]
RewriteRule ^.*$ http://apps.example.com%{REQUEST_URI} [R,L,P]
</VirtualHost>
<VirtualHost *:80>
ServerName apps.example.com
DocumentRoot /home/sites/example.com/public_html
</VirtualHost>

generic non-www to www, and non-http to https

I have the following code for my .htaccess file that I've picked up from here and tried adapting it as I understand from .htaccess, yet I can't seem to get it to work (or maybe the browser has cached it but I can't seem to clear it).
Options -Indexes
Options +FollowSymlinks
<IfModule mod_rewrite.c>
########## FORCE SSL ##########
RewriteEngine On
RewriteBase /
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}/$1 [L,R=301]
</IfModule>
I want to make it as generic a possible so I can simply copy and paste it to any site I make so no need to edit it each time - I guess I could also do this in PHP but I think it would be good if .htaccess is also there.
Another point is, can .htaccess be read if i were to go to www.domain.com/.htaccess or do I need to cover that in a 'deny all' kind of thing?
The second argument to the RewriteCond must be a regex, so it cannot contain a variable.
Try adding the following to your .htaccess file in place of the rules you had
#capture top level domain (.com or .co.uk)
RewriteCond %{HTTP_HOST} ([-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk))$ [NC]
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ - [E=MY_TLD:%2]
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
#if host does not start with www
RewriteCond %{HTTP_HOST} !^www\.[-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk)$ [NC]
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
Shortest version would be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]
Only (somewhat) downside would be that subdomain.example.com gets redirected to www.subdomain.example.com

Rule not redirecting .co.uk to .com

I have been trying to get my site, when a visitor goes to .co.uk to be automatically redirected to .com. As well as if they go to domain.com to be taken to www.domain.com
I have the code below in my httpd.conf. It appears to be working with domain.com to www.domain.com but not domain.co.uk or www.domain.co.uk to www.domain.com
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^domain.com [NC,OR]
RewriteCond %{http_host} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
If your rewrite rules are in an htaccess file, this should work. If they are in a vhost file (or the Apache httpd.conf itself) try to remove the /.
And try to always be case sensitive (get used to because most of languages are case sensitive it's a good habit to take):
If in a .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
If in a vhost or httpd.conf file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,NC]
And please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
Tell me if it works.
Is the name of HTTP_HOST case-sensitive? The RewriteCond documentation always lists it as HTTP_HOST, not http_host. The NC flag won't help with that, since it applies to the string values, not variable names.