Rewrite to a new domain? with url query? not working. :( help - apache

I have this rewrite rule...
Redirect all initial request from world.example.com to web.example.com
RewriteCond %{HTTP_HOST} ^world\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(.*)$ https://web.example.com$1 [R=301,L]
Which works great. But, some of my applications has...
https://world.example.com/approvals/?id=b47b5256567
Unfortunately, it is not being redirected properly to web.example.com. Instead, it just goes to web.example.com without the query params.
How could I make all request redirect properly to web.example.com along with the query params?
Basically, what it should do...
https://world.example.com/approvals/?id=b47b5256567
then
https://web.example.com/approvals/?id=b47b5256567
Just changing the world to web and passing the query string.
Help

You don't need to use the complicated rewrite engine to do this. Just use Redirect:
<VirtualHost *>
ServerName world.example.com
Redirect permanent / https://web.example.com/
</VirtualHost>
The Redirect directive automatically preserves everything that comes after what you're redirecting.

You forgot to use the "Query String Append" flag [R=301,L,QSA].

We can do it with Nginx, too.
server {
listen 80:
server_name: world.example.com
# URL redirect
#rewrite ^ http://web.example.com$request_uri? permanent;
rewrite ^ $scheme://web.example.com$request_uri permanent;
#rewrite ^ $scheme://web.example.com permanent;
#rewrite ^ $scheme://web.example.com;
}
Reference:
Nginx Redirect (Rewrite) Old Domain To New Domain With HTTP 301

Related

Redirect documentroot to another folder in conf apache or .htccess

Good Day
I want to rewrite a url when request a subdirectory
When the user request
172.0.0.1/url
i want to rewrite to make this url point to
172.0.0.1/url/url
or the documentroot is in /var/www/html/url/url make this point to
172.0.0.1/url
The trick here is to avoid creating a rewrite and a redirect loop. You can put this code in your root folder htaccess
RewriteEngine On
# Redirect /url/url/xxx to /url/xxx
RewriteCond %{THE_REQUEST} \s/(url)/url/(.*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,L]
# Internally rewrite back /url/xxx to /url/url/xxx
RewriteRule ^(url)/((?!url/).*)$ /$1/$1/$2 [L]
You can use .htaccess to redirect /url to /url/url
Just Create .htaccess file at /var/www/html/ with following contains:
RewriteEngine on
RewriteRule ^/?url/(.*)$ /url/url/$1 [R,L]
This will Redirect all incoming requests at /url and redirect them to /url/url
But you cannot redirect requests coming at /url/url to /url Because we are already redirecting request at /url, this will result in catch 22 and browser will keep redirecting back and forth between /url and /url/url.

redirect https://domain to https://www.domain in httpd24

I'm trying to redirect URL responses from https://domain.com to https://www.domain.com in the apache24 configuration, I'm using Redhat Server.
I have tried to create this Rewrite Condition but its not working
RewriteCond %{HTTP_HOST} ^(domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
I have fixed the Issue by using Directive IF Condition powered by Apache2.4
I have added this code to my ssl.conf file where it redirected all requests sent to https:// domain .com to https:// www .domain .com . Add this code before the Tag :
<If "%{HTTP_HOST} != 'www.domain.com'">
Redirect "/" "https://www.domain.com/"
</If>
Thank you all for your comments and trying to fix my issue, really its something I appreciate it. Please take a look # for more information about httpd24 directives https://httpd.apache.org/docs/2.4/mod/directives.html
() is a capturing group. You don't want that when you check HTTP_HOST unless you're going to use it as part of the new URL. Also make sure to add the ^ (start of string) and $ (end of string) on your RewriteRule.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
You can test your htaccess here http://htaccess.mwl.be/ without having to upload and refresh your site.

.htaccess redirect to example.com except for directory redirect to example.com/example

Variations on this question have been asked, and so I apologize in advance -- I simply cannot get this to work. I know the devil is in the details with the .htaccess, so I'm sure I'm missing something small.
Here's what have:
I have a page: example.com/ which redirects all traffic to its https equivalent. I'm building the next version of the same site inside of the directory example.com/example.com/ I'm trying to write a redirect that will send all traffic to its https equivalent except for the directory that I'm working in, which should be left alone. My problem is, whenever I try to access example.com/example.com/ I get redirected to the 404 of example.com (which makes sense). I just can't get the redirect to not loop indefinitely.
I currently have a redirect from http to https for example.com as follows:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
This works just fine, and only causes troubles when I try to access the subfolder.
tl;dr
Here's what I need:
http://example.com/ REDIRECTS TO https://example.com/
http://example.com/[anything else] REDIRECTS TO https://example.com/[anything else]
https://example.com/example.com/ SHOULD NOT REDIRECT
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !example.com
RewriteRule ^(.*)$ https://example.com/$1 [L]

Rewriting from https to http with htaccess how to remove port number?

Im having this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on [AND]
RewriteCond %{REQUEST_URI} /products-page/bathroom-amenities/nourish-a-spa-line/
RewriteRule (.*) http://%{HTTP_HOST}/bathroom-amenities/286-nourish.html
</IfModule>
what this does is to redirect the user from:
https://www.mydomain.com/products-page/bathroom-amenities/nourish-a-spa-line/
to:
http://www.mydomain.com:443/bathroom-amenities/286-nourish.html
This Code is redirecting from a https URL to a http URL, but for this to work I need to remove the port from the result URL.
Can anyone help me with this?
User SERVER_NAME instead of HTTP_HOST
Be careful with the use of UseCanonicalName setting, most likely you want to have it set to Off

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.