htaccess 301 redirect to different domain - apache

Trust me, I've searched all over for a solution to my problem, so I don't believe this is a duplicate question.
I need to do the following:
301 Redirect blog.example.com to blog.newexample.com.
What I have is close to working. When I go to the homepage of blog.example.com it will redirect perfectly to blog.newexample.com. However, if I go to blog.example.com/directory/page it does not direct me to blog.newexample.com/directory/page.
Here is what my .htaccess looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !newexample.com$ [NC]
RewriteRule ^(.*)$ http://blog.newexample.com/$1 [L,R=301]
</IfModule>
# END WordPress

I'm not sure to understand what your problem is, but this works just fine :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newexample.com$ [NC]
RewriteRule ^(.*)$ http://blog.newexample.com/$1 [L,R=301]
</IfModule>
The URL http://website.com/hello/world/ redirects me to
http://blog.newexample.com/hello/world/.

Related

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Apache .htaccess RewriteRule not including subdirectory when redirecting non-www to www url

I've setup a rewrite rule in order to direct domain.com traffic to www.domain.com using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
From my limited understanding, the $1 in the RewriteRule should match anything after domain.com/ and then be placed at the end of my rewritten url www.domain.com/.
For example domain.com/abc should become www.domain.com/abc.
However this is not working. If I browse to domain.com/abc then this path isn't rewritten at all and I just get domain.com/abc without the www.
I've done lots of reading just to figure out that the $1 should be taking care of this, from my understanding.
Can anyone explain why it isn't working as I suspect it should be? Thanks.
It turns out that because I was using Wordpress AND W3 Cache plugin that I was simply putting the code in the wrong place in my .htaccess file.
The code should be placed within the # BEGIN Wordpress codeblock and before the existing rules that Wordpress already has in place.
The existing code should look something like this:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
I added my original code to this as follows:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
This fixes the issue I was describing above. Thanks to the commenters for sparking my thought process.

301 redirect with query parameters isn't working in .htaccess

I have a new website recently uploaded on a domain name and I want to redirect individually all my previous pages to the new ones. (they aren't more than 20)
WHat i've tried to accomplish is the following, but for some reason it isn't working:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=16&page=PRICES_SKI_PACKS$
RewriteRule ^(.*)$ http://www.skibansko.bg/prices-ski-packs/ [L,R=301]
Old url: http://skibansko.bg/index.php?id=16&page=PRICES_SKI_PACKS
New url: http://www.skibansko.bg/prices-ski-packs/
It simply is redirecting me to the home page of the new website.
The old website was not on Wordpress and the new one is on Wordpress
The old and the new websites were on the same domain as you can see from the url's.
Probably the WordPress rewriting rules are causing it, you can check them here:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And my whole .htaccess looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=16&page=PRICES_SKI_PACKS$
RewriteRule ^(.*)$ http://www.skibansko.bg/prices-ski-packs/ [L,R=301]
Thank you all in advance!

Mod_rewrite working on http but not https

I have a mod_rewrite that works fine on http, but not on https.
Some answers on stack overflow to similar questions suggest adding RewriteBase /, but I already have that.
I'm hosted on godaddy on a shared server. I have contacted godaddy 3 times and they say they can't do anything.
Here is an example of the rewrite:
URL Without Rewrite:
https://www.funded.today/stats/?p=bloodstained-ritual-of-the-night
Mod_Rewrite URL:
https://www.funded.today/stats/iga/bloodstained-ritual-of-the-night
My htaccess file is the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?funded\.today$ [NC]
RewriteRule ^$ https://www.funded.today/%{REQUEST_URI} [R,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^stats/(.*)/(.*)$ /stats/?p=$2 [P,QSA]
# BEGIN s2Member GZIP exclusions
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|\?|&)s2member_file_download\=.+ [OR]
RewriteCond %{QUERY_STRING} (^|\?|&)no-gzip\=1
RewriteRule .* - [E=no-gzip:1]
</IfModule>
# END s2Member GZIP exclusions
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.*)/(.*)$ - [L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help or ideas on this is greatly appreciated!

How to redirect specific links to https:// everything else to http:// using .htaccess?

I am trying to use htaccess to automatically direct all requests to certain pages to https and everything else to http://
Here is the code I've got the code to force ssl working below
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]
This is working as expected and redirects to the ssl versions of those pages. I then tried adding the following to redirect everything else to http:
RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]
This code results in a redirect loop when going to /va-purchase-request /va-homebuyers-guide and /va-streamline-refinance and does not redirect the other pages at all.
I'm totally stuck with this and any help would be massively appreciated!
Edit:
I also have this code in the .htaccess file added by wordpress... Could it be interfering with the other redirects?
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]