Abstraction of Apache VirtualHost URL's - .htaccess

So I have a couple of mod_rewrite rules to make URL's prettier.
By this I mean I wish to change a url that is entered as such:
http://test.domain.com/cheese/wine/
to a background association towards the test.php as such:
http://test.domain.com/test.php?client=test&page=cheese/wine
PS: The client variable above is actually the [test].domain.com.
According to the rewrite log all the RewriteRule's seem to work fine but all I get is a 400 error.
vHost file:
<VirtualHost *:80>
ServerName *.domain.com
DocumentRoot /var/www/dir/
...
RewriteEngine on
# drop www. from subdomains
RewriteCond %{HTTP_HOST} ^www\.(.*)\.domain.com$
RewriteRule ^/?(.*)$ http://%1.domain.com/$1 [R=301]
# rewrite x.domain.com/abc to abc?client=x
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^/?(.*)$ $1?client=%1 [QSA]
RewriteRule ^/?(.*)$ test.php?page=$1 [QSA,L]
...
</VirtualHost>
Does anyone have any ideas?
Edit: The request hostname (*.domain.com) needs to stay intact because (www.)domain.com is treated in a different manner.

Your code generates an infinite loop.
<VirtualHost *:80>
ServerName *.domain.com
DocumentRoot /var/www/dir/
...
RewriteEngine on
# drop www. from subdomains
# also drop www. from main domain
RewriteCond %{HTTP_HOST} ^www\.(.*\.)?domain.com$
RewriteRule ^/?(.*)$ http://%1domain.com/$1 [R=301]
# rewrite x.domain.com/abc to abc?client=x
RewriteCond %{REQUEST_URI} !^test\.php
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^/?(.*)$ test.php?page=$1&client=%1 [QSA,L]
...
</VirtualHost>
I also modified the code so www.domain.com/ redirect to domain.com/

Related

htaccess Canonical URL and HTTP->HTTPS redirects (custom url)

I hope so you could help me solve this issue. I am trying to setup my apache server redirects, but without success. I am trying to do this at at once:
http://www.example.com -> redirects to -> https:/ /example.com
http://example.com -> redirect to -> https:/ /example.com
https://www.example.com -> redirect to -> https://example.com
(if the URL contains file extensions like .php/.html I want to remove them) LIKE: https:/ /example.com/portfolio.php -> to be replaced to -> https://example.com/portfolio
URLs can also contain 1 or 2 URL params and I want to replace the url separated by / like that: https://example.com/portfolio.php?id=1&t=Max -> to be replaced to ->
https://example.com/portfolio/1/Max
Also all redirects should use R=301 if possible.
Here is what I have so far, but it's not working.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^portfolio/(\d+)/(.*) portfolio.php?id=$1 [QSA,L]
RewriteRule ^portfolio portfolio.php [QSA,L]
RewriteRule ^about about.php [QSA,L]
Please help me solve this issue. It took me so much reading but never did it right. The main problem is that Google Crawlers recognize my pages as duplicated, and I have canonical URL problems which I want to resolve.
Thank you for the help!
Not perfect, but conceptually, this will take care of part of it. Redirect http to https, solving 1, 2, & 3.
File found in /etc/apache2/sites-enabled on Linux
Listen 443
Listen 80
IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
redirect / https://example.com
</VirtualHost>
<VirtualHost*:80>
ServerName www.example.com
redirect / https://example.com
</VirtualHost>
<VirtualHost*:443>
ServerName www.example.com
redirect / https://example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
**Put all the other stuff you need here**
</VirtualHost>
There are a lot of things to consider, depending on your configuration.
You’ll need to restart Apache after modifying the file.
Try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)
RewriteRule ^(.*)$ /$1/%1/%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+)/(\d+)/(.*)$ /$1.php?id=$2&t=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
The first three lines after RewriteEngine on is to make sure all requests are https://example....
The next two lines is to remove both html & php extentions.
The next two line capture any query string /portfolio?id=1&t=max and rewrite it to /portfolio/1/Max externally.
The next two line capture any URI like /portfolio/1/Max and rewrite it to original path internally.
The rest of lines are to redirect the rest of URIs and check whether they are .php or .html internally.
Note: clear browser cache and then test

Htaccess Trouble redirecting wildcard.domain.com to domain.com/wildcard

I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]

How to automatically redirect HTTP to HTTPS on Apache servers?

Environment Centos with apache
Trying to setup automatic redirection from http to https
From manage.mydomain.com --- To ---> https://manage.mydomain.com
I have tried adding the following to my httpd.conf but it didn't work
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
Any ideas?
I have actually followed this example and it worked for me :)
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
Redirect permanent / https://mysite.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Then do:
/etc/init.d/httpd restart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
http://www.sslshopper.com/apache-redirect-http-to-https.html
or
http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html
Searched for apache redirect http to https and landed here. This is what i did on ubuntu:
1) Enable modules
sudo a2enmod rewrite
sudo a2enmod ssl
2) Edit your site config
Edit file
/etc/apache2/sites-available/000-default.conf
Content should be:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile <path to your crt file>
SSLCertificateKeyFile <path to your private key file>
# Rest of your site config
# ...
</VirtualHost>
Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.
3) Restart apache2
sudo service apache2 restart
Using mod_rewrite is not the recommended way instead use virtual host and redirect.
In case, if you are inclined to do using mod_rewrite:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same
location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context
Reference: Httpd Wiki - RewriteHTTPToHTTPS
If you are looking for a 301 Permanent Redirect, then redirect flag should be as,
R=301
so the RewriteRule will be like,
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add
Redirect permanent / https://[your-domain]/
I needed this for something as simple as redirecting all http traffic from the default apache home page on my server to one served over https.
Since I'm still quite green when it comes to configuring apache, I prefer to avoid using mod_rewrite directly and instead went for something simpler like this:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
SSLEngine on
...
</VirtualHost>
I like this because it allowed me to use apache variables, that way I didn't have to specify the actual host name since it's just an IP address without an associated domain name.
References:
https://stackoverflow.com/a/40291044/2089675
Server version: Apache/2.4.29 (Ubuntu)
After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz
To enable SSL, type (as user root):
a2ensite default-ssl
a2enmod ssl
In the file /etc/apache2/sites-available/000-default.conf add the
Redirect "/" "https://sub.domain.com/"
<VirtualHost *:80>
#ServerName www.example.com
DocumentRoot /var/www/owncloud
Redirect "/" "https://sub.domain.com/"
That's it.
P.S: If you want to read the manual without extracting:
gunzip -cd /usr/share/doc/apache2/README.Debian.gz
This code work for me.
# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
Please try this one in apache Virtualhosting configuration
and then reload apache service
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
for me this worked
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
This worked for me:
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]

How to redirect directory to sub-domain, and www sub-domain to non-www sub-domain?

NOTE: Using <VirtualHost> parameter only.
How do I redirect a directory (say example.com/groups/) to a sub-domain (say groups.example.com), and also the www of that sub-domain (www.groups.example.com) to the non-www URL of that sub-domain (groups.example.com)?
Precisely (⇒ stands for 'should 301 redirect to'):
example.com/groups/ ⇒ groups.example.com
www.groups.example.com ⇒ groups.example.com
I have read that using <VirtualHost> is better than all those redirection rules that are usually used (i.e. that using redirection engine is pretty heavy on disk i/o). So, I would like to have the above problem solved using <VirtualHost> in .htaccess / httpd.conf if possible. Thanks!
Something like this should work
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/(\w+)/?$ http://$1.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.(\w+)\.example\.com$
RewriteRule ^/(.*)$ http://%1.example.com/$1 [R=301,L]
</VirtualHost>
or specifically for groups
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/groups/?$ http://groups.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.groups\.example\.com$
RewriteRule ^/(.*)$ http://groups.example.com/$1 [R=301,L]
</VirtualHost>

301 redirect match problem

Ok I am serving two domains off of one box. The domains are:
www.old.org and www.new.com. As it is now, all of the files and dirs are the same on both domains.
I want to make it so that IF someone goes to www.old.org/folder_a/file_b.php they are 301'ed to www.new.com/folder_a/file_b.php.
I've already tried in the htaccess:
RedirectMatch 301 ^/ http://www.new.com/
But that give a 301 loop because the 301's condition still applies after the 301 is enacted. I think I want to do something that uses rewritecond %{HTTP_HOST} ^.*old.org$ so that only url's at old.org or www.old.org will be affected, but I'm not sure how to do this.
If you have access to the apache vhost configs use those instead of the .htaccess:
<VirtualHost *:80>
ServerName www.old.org
Redirect permanent / http://www.new.com/
</VirtualHost>
If you really must use an .htaccess the following will do:
RewriteEngine On
RewriteCond %{SERVER_NAME} =www.old.org [NC]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]
Some thing like this should do:
RewriteCond %{http_host} ^www\.old\.org$ [NC]
RewriteRule ^/(.*) http://www.new.com/$1 [R=301]

Resources