htaccess and WordPress problems - wordpress

I wanna redirect all my wordpress pages from one domain to another, such as:
from: http://domain1.com/page/
to: domain2.com/page/
So I added to htaccess of domain1.com:
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
But I want to make certain files exceptions to the above rule, so I also added:
RewriteCond %{REQUEST_URI} ^(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html).*
but the second rule doesn't seem to work. :(

Those aren't exceptions, those are "the request MUST be images/javascripts/stylesheets/etc in order to redirect", so you've got it backwards. You want a ! in there:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html)
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
Though I'm assuming you're way overreaching with the regular expression there, some of those look like you are matching against only the extension, so:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global)
RewriteCond %{REQUEST_URI} !\.(js|html?|png|je?pg|gif)$ [NC]
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]

Related

Redirect any directory if it has a specific query string

I am trying to redirect all pages with a specific query string to another page.
So many pages have this query string, but the URI's are different.
I want to capture any URI and if it has this query string to redirect it but nothing I am doing is working.
So just to be clear:
http://example.com/?redirectme=1
would redirect to
http://example.com/newpage
while also:
http://example.com/randompage/rtandomsirectory?redirectme=1
would also redirect to
http://example.com/newpage
I am sure there must be 2-3 lines I can put in the htaccess to do this but it is not working.
This is a wordpress site if that should make a difference.
Here is what I have tried:
RewriteCond %{REQUEST_URI} ^/(.+)
RewriteCond %{QUERY_STRING} ^redirectme=1$
RewriteRule ^/?$ http://example.com/? [R=301,L]
and
RewriteCond %{REQUEST_URI} ^/(.+)
RewriteCond %{QUERY_STRING} ^redirectme=1$
RewriteRule (.*) http://example.com/? [R=301,L]
different variations:
RewriteCond %{REQUEST_URI} (.+)
RewriteCond %{QUERY_STRING} ^redirectme=1$
RewriteRule (.*) http://example.com/? [R=301,L]
and for the first line:
RewriteCond %{REQUEST_URI} (.*)
And so on....
Any experts on this here?
Thanks
You can use this redirect rule as top rule in your site root directory:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirectme=1$ [NC]
RewriteRule ^ /newpage? [R=301,L]
# remaining rules below this line

WordPress htaccess ignoring my redirect exclusions

I have the following in my htaccess file for my WordPress site:
RewriteCond %{HTTP_HOST} ^libertychurchnyc\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.libertychurchnyc\.com$
RewriteCond %{REQUEST_URI} !^libertychurchnyc\.com/community-groups/
RewriteCond %{REQUEST_URI} !^libertychurchnyc\.com/podcasts/
RewriteRule ^(.*)$ "http\:\/\/libertychurch\.com\/$1" [R=301,L]
The site redirects perfectly BUT my exclusions for community-groups and podcasts is completely ignored.
I know I'm missing something here, but after going through a ton of posts here, I finally had to break down and ask what I've done wrong.
Thank you!

Within .htaccess File https is Off

I am wanting to redirect all my pages to https. Currently it is doing this but some websites when doing test state that I have to many redirects. This is basically what I have set up within my .htaccess file (for Wordpress)"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
NOTE: I also have another redirect within this file for another reason, as I have changed my URLs and wanting them all to redirect using a 301:
RewriteBase /
RedirectMatch 301 /market/(.*)/ /product/$1
I'm basically wondering if I have done it all correctly or is there a simpler way so there is not so much redirects. I also notice within my Plesk panel there is an option for this. Any help would be great.
I suggest using thos code in your htaccess file to force HTTPS redirection (make sure to adjust URLs to your actual ones):
RewriteCond %{HTTP_HOST} !^domain\.local$ [NC]
RewriteCond %{HTTP_HOST} !^staging\.domain\.com$ [NC]
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

Switching http to https using mod_rewrite

I know there are lots of similar questions, but I spent more than 2 days investigating it with no success, so you are my last hope )
I wrote following rewrite rules:
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^((/member/settings)?/orders/?)$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteRule \.(gif|jpe?g|png|css|js|eot|woff|ttf|svg)$ - [NC,L]
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^((/member/settings)?/orders/?)$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [NC,QSA,L]
I want URLs example.com/orders/ and example.com/member/settings/orders/ always redirect to HTTPS, and all others force using HTTP. Also all queries should be forward to index.php.
And, obviously, all media files should stay in same protocol as they was requested.
When I'm requesting http://example.com/orders/ first it redirects to https://example.com/orders/, but then redirects second time to http://example.com/index.php?/orders/ . What actually is expected, but problem is that 'index.php' appears in browser's address bar.
The question is how to get rid of 'index.php' in browser's address bar?
Any help will be appreciate.
then redirects second time to http://example.com/index.php?/orders/ .... I have no idea from where 'index.php' comes up in URL...
Because you have a RewriteRule to do just that:
RewriteRule ^(.*)$ index.php?/$1 [NC,QSA,L]
Not sure what you're trying to accomplish in that block, but try getting rid of it.

How to redirect HTTPS to HTTP for some pages using .htaccess

I am facing URL rewriting problem using .htaccess.
I have to redirect all the URLs to HTTPS except 4 URLs. Below is the code in my .htaccess file:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_REFERER} !^https(.*)/(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php?view=default [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php?view=news [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC, L]
# Require SSL on all other pages
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}r%{REQUEST_URI} [R=301, NC, L]
I need to disable HTTPS for following URLs:
mydomain.com/
mydomain.com/index.php
mydomain.com/index.php?view=new
mydomain.com/index.php?view=new&abc=xyz&aaa=bbb…
mydomain.com/index.php?view=default
For above 5 URLs, I want to redirect in http, but it does not work. It redirects all the URLs to HTTPS only.
The RewriteCond conditions act like an "and", so it's not going to match all 4 file patterns at the same time. One option is separate sets of rules - I like to do that when figuring it out. Another option is to get an "or" pattern working.
The querystring is handled in the RewriteCond, instead of down in the RewriteRule.
I'm not sure what you needed to test on HTTP_REFERER, so I left it out.
This separates out to two rules to keep it simpler. The first does the plain slash or optional index.php with no parameters. The second checks the querystring for view = default or new.
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/(index\.php)?$ [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC]
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} ^view=(default|new(&.*)?)$ [NC]
RewriteCond %{REQUEST_URI} ^/?index\.php$ [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301, NC]
The querystring condition checks for view=default exactly, or view=new or view=new&any=other... The optional &.* means the next thing has to be an ampersand or nothing, which prevents view=new2 from matching by mistake.
Note the user might get a browser warning about redirecting to an "insecure" page, when you redirect them from https to http. Redirect warning isn't a big concern anymore; I just remember it from IE6 days.
How to redirect from HTTPS to HTTP without annoying error messages
EDIT:
I added a condition to the first rule for an empty querystring. I realized that was matching any querystring without it.
I was also faced that issue and for me this help
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} ^(/check-out|/payment)$
RewriteRule (.*) https://www.my_site.com/$1 [R,L]
You need to change pages name & your site url.

Resources