Apache rewrite all incoming to same address - apache

Hi I want to direct all traffic on port 443 to teh same adress but I want to kepp the parameters.
I have tried to read how to do it and right now I have something like this
<Virtualhost x.x.x.x:443>
RewriteEngine on
RewriteCond %{REQUEST_URL} !^(.*)example2.com/test/(.*) [NC]
RewriteRule ^(.*)$ https://example1.example2.com/test/$1 [R,L]
So basically I want to rewrite everything that doesn't have example2.com/test/ in the URI
Example if I get www.example.com?parameter=5
I want it to be https://example1.example2.com/test?parameter=5

The %{REQUEST_URI} is only the URI and not the hostname. So there will never be example.com in the variable, and thus the condition will always be true (unless you're actually going to http://example1.example2.com/example2.com/test/).
Try:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(.*)example2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/(.*) [NC]
RewriteRule ^(.*)$ https://example1.example2.com/test%{REQUEST_URI} [R,L]

Related

Double conditions redirection

I'd like to permanently redirect my pages to https + www.
I'm using the code below but it doesn't work when I enter https://example.com. It does nothing and displays the page without adding the www.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
There is no problem with the given redirect rule. It seems problem is that OP is executing shown rule from <VirtualHost *:80> section. Those rules will obviously won't fire if request is https://. OP can do one of the two thing to fix the problem:
Keep same redirect rule in <VirtualHost *:443> section as well - OR
Keep rule in a common place like .htaccess
Try it like this:
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your cache before testing this.

Redirect to subfolder with htaccess

I'm trying to write a .htaccess-file that redirect requests of example.com or www.example.com to: https://example.com/game1
But I can't get it to work.
I have this in my .htaccess:
# ReDirect to current instance of game
RewriteEngine On
# RewriteCond %{HTTPS} != On
SetEnvIf PAGE_PATH "game1"
RewriteRule ^/?(.*) https://%{SERVER_NAME}/%{ENV:PAGE_PATH}/$1 [R,L]
The PAGE_PATH variable does not work and if I use setup2 apache changes the URL but the redirect fails and I can't call the correct URL either. I get error: example.com redirected you too many times (ERR_TOO_MANY_REDIRECTS).
Setup2 for .htaccess:
# ReDirect to current instance of game
RewriteEngine On
# RewriteCond %{HTTPS} != On
RewriteRule ^/?(.*) https://%{SERVER_NAME}/game1/ [R,L]
How can I configure my desired behavior?
You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1/game1%{REQUEST_URI} [NE,L,R]
This will redirect :
example.com
or
www.example.com
to
https://example.com/game1/
Try this:
RewriteRule ^(?>/?)(?!game[1-9]\d*(?:$|/))(.*) https://%{SERVER_NAME}/%{ENV:PAGE_PATH}/$1 [R,L]
It will only redirect if the URL does not start with /gameN/ where N is any whole number. It first puts the slash in a once-only subpattern so, if matched, it cannot be "unmatched" by backtracking and break the next part. The next part is a negative look-ahead assertion.
Separate out the domain and https checks and place them before the game path redirect, since they should be permanent redirects whereas game should be temporary. You're better off hard-coding the domain in that check since SERVER_NAME is affected by UseCanonicalName which is off by default. Replace localhost with your dev server's domain.
RewriteCond %{HTTP_HOST} !^(?:localhost|example\.com)$
RewriteRule .* https://example.com/$0 [L,R=301]
RewriteCond %{HTTPS} ^off$
RewriteRule .* https//%{SERVER_NAME}/$0 [L,R=301]

htaccess redirect if hostname is NOT

I'm running a server with multiple vhosts and phpMyadmin is set up as an alias which can be access via anydomain.com/phpmyadmin. I would like to use an .htaccess redirect rule so that if phpmyadmin is NOT accessed on the server-admin-url, the visitor is redirected to, say, Google.
The correct URL would be: https://server.domain.com:9090/phpmyadmin
Could anyone help me, please?
Thanks!
The easiest way you can do this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]
Maybe you want also check the port, then use
RewriteCond %{SERVER_PORT} !^9090$ [OR]
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]
Update
Reading again your question I see you're also trying to switch from http to https.
I suggest to add a check if https is off:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]
If your VirtualHost is configured with https you should pay particular attention to how VirtualHost matching works

.htaccess change domain but keep path

i don't even know whether this is possible or not.
i've 3 domain names:
mytest.com
test88.com
test99.com
mytest.com is the main domain where all the content is located. in my case it is wordpress which is installed on that webspace.
my htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [R,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [R,L]
i want to keep the domainname in case a visitor goes to test88.com but i also want to keep the rest of the path. It should look like this in the address-bar:
http://www.test.88.com/wp/?page_id=10&test=test88
ist this possible?
thanks in advance
Do you want the content from mytest.com show up under the test88.com and test99.com, basically creating duplicates ?
In that case you probably don't want to mod_rewrite to redirect [R], but to reverse-proxy [PT] the content from the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [PT,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [PT,L]
Make sure mod_proxy is installed on your Apache.

force SSL and no WWW on Apache

I'm having trouble coming up with the proper syntax to accomplish both forcing the SSL and no WWW.
EDIT
I've been able to accomplish each task separately but when combining the two i find myself stuck in a redirection loop.
working syntax to force no WWW:
RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
My attempt to force no WWW and SSL
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule (.*) https://domain.com/$1 [R=301,L]
Thanks for any suggestions!
For SSL you could use something like:
Redirect / https://domain.com/
Place this only in the section of your virtual host you configure for HTTP, not HTTPS, to not run clients into endless loops.
Here's what I'm using on one of my sites - it seems to work a little better than most of the other methods I've seen:
# The code below tells apache to always require secure (ssl/tls) connections
# to the website. If a client tries connecting over port 80 (http://),
# then the client will be redirected to https:// (over port 443).
RewriteCond %{REMOTE_ADDR} !127\.0\.0\.0
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
For the no-www rule, check out the .htaccess files on any open-source CMS, like Drupal or Wordpress, to see some of the best practices.
By 'no WWW' I assume you mean you want to remove any 'WWW.' prefix of the hostname? Try this:
RewriteCond "%{HTTP_HOST}" "^(?:www\.)?(.*)" [NC]
RewriteCond "%{HTTPS}" "=on"
RewriteRule "(.*)" "https://%1$1" [R=301,L]
If you're doing this in a .htaccess file, change that last line to
RewriteRule "(.*)" "https://%1/$1" [R=301,L]
If you want to be able to remove the 'WWW.' prefix regardless of SSL-ness or not, try this:
RewriteCond "%{HTTP_HOST}" "^(?:www\.)?(.*)" [NC]
RewriteCond "%{HTTPS}" "=on"
RewriteRule "(.*)" "https://%1/$1" [R=301,L]
RewriteCond "%{HTTP_HOST}" "^(?:www\.)?(.*)" [NC]
RewriteRule "(.*)" "http://%1/$1" [R=301,L]
I found this to work for a couple of my client sites:
# Force SSL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
# Rewrite all http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}