Redirecting all but one pattern of file to new site - apache

If I have http://example.com and I want to redirect all traffic except orange.1.php, orange.2.php, orange.32.5.php, etc to http://newexample.com, how would I write the .htaccess? Just to be clear, http://example.com/blah should redirect to http://newexample.com, not to http://newexample.com/blah. I've googled for an hour trying different things, and none seem to work.
I thought this should work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^orange\.[0-9]+.*$
RewriteRule ^/?.*$ "http:\/\/newexample\.com" [R=301,L]
Thanks!

REQUEST_URI always starts with a forward slash.
You can use this refactored shorter rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule !^orange\.\d+\.php$ http://newexample.com/? [R=301,L,NC]

Related

How to force SSL and WWW in one redirect?

I'm wanting to minimise redirects whilst forcing https and www.
Trying to get...
http://example.com
...to go straight to...
https://www.example.com
... in one redirect. Not three, two, or even the 19 I had at one stage! I'm stuck at having one redirect for the www. bit, and then another redirect for the https bit.
Currently have this in my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301]
I was about to concede defeat, feeling that it's not possible to combine two rules into one... but then I discovered Amazon.com have it implemented! Wondering how? Does this involve doing something outside of .htacess?
Screenshot of Amazon.com and their straight-to-the-point redirect
To force https and www in one redirect, you can use
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
Try it like this,
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect from https:// to https://www using .htaccess

I am trying to get my site to redirect all traffic to https://www.mylittleblackebook.com no matter how the URL is typed into the browser. There are six ways someone can type the URL into a browser. They are:
https://www.mylittleblackebook.com
https://mylittleblackebook.com
http://www.mylittleblackebook.com
http://mylittleblackebook.com
www.mylittleblackebook.com
mylittleblackebook.com
All of these URL’s will work EXCEPT FOR the second one. I cannot get the .htaccess file to redirect https:/mylittleblackebook.com to https://www.mylittleblackebook.com. Here is the code in my .htaccess file:
RewriteEngine on
# require SSL without mod_ssl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code works for 5 out of the 6 ways. Can anyone tell me how to get it to work for all of them?
Thank you for taking the time to look this over.
Your code looks correct, not sure why it doesn't work. I would try this instead, it's in the opposite order and a bit different:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) https://www.%{HTTP_HOST}/$1 [L,R,NE]

Redirect 301 not working for all pages on Apache

I've already looked at many samples and tutorials how to set up those 301 redirects on Apache and can't figure out why only the second one is working:
Options +FollowSymlinks
RewriteEngine on
#doesn't work:
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^page-still-exists.htm$ "http://www.new.com/new-target-page.htm" [R=301,L]
#works:
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^page-does-no-longer-exist.htm$ "http://www.new.com/" [R=301,L]
#works:
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^folder/otherpage.htm$ "http://www.new.com/" [R=301,L]
#works:
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^/?$ "http://www.new.com/" [R=301,L]
#doesn't work:
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^somepage.htm$ "http://www.old.com/some-page.htm" [R=301,L]
I have no idea why only the second one is working. The only difference I can see is, that in the second case the old page does no longer exist on the old domain. But whenever I want to redirect any still existing page from the old domain to the new domain the page on the old domain is still used.
Any input is much appreciated because this is slowly driving me crazy :)
EDIT: I added the complete htaccess file.
EDIT 2: So I removed almost all redirects and currently my htaccess looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.new\.com\/$1" [R=301,L]
The only redirect that is working is the simple one from old.com to new.com. A redirect like old.com/page.htm to new.com or even new.com/page.htm is not working. And actually I really don't know where this redirect is actually coming from... Can a 301 really be so complicated?
Can you try just this rule and see how it behaves:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^page-still-exists\.htm$ http://www.new.com/new-target-page.htm [R=301,L,NC]

Redirecting subdomain to file without hardcoding url

Actually topic is simple and answered million times but, somehow doesn't work on me.
What I try to do is redirect "m.domain.com" and "mob.domain.com" to a file "domain.com/folder/mobile.htm"
But looks like any code conflicts with my current code which removes "www" from beginning.
My current code is
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
# this is for webmail forward #
RewriteCond %{REQUEST_URI} /mail/?$ [NC]
RewriteRule . /webmail/ [L]
How can I redirect m and mob domains without conflicting with this code and without hardcoding "domain.com" ?
Thanks for your helps in advance.
Put this rule on top of your existing rules just below RewriteEngine On line:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:m|mob)\.(.+)$ [NC]
RewriteRule ^$ http%1://%2/folder/mobile.htm [L,R=301]

Forcing WWW with httpd.conf rather than .htaccess

I've read up on many Force WWW. tricks using htaccess. (As Below)
Rewritecond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) http://www.domina.com/$1 [R=301,L]
I'm wondering if there is a method to force WWW using httpd.config across the entire site.
The htaccess works a treat, but I have many subdirectories, most of them include there own .htacess files, which breaks the force www in that particular subdirectory.
So I was thinking, is there a way to force this "www." into httpd.config, across the entire site without editing .htaccess?
Thanks.
This should do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And to maintain HTTPS too:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works for me
# direct all variation of FQDN to www.epoch.co.il
RewriteEngine on
#RewriteLogLevel 3
#RewriteLog "/var/log/httpd/epoch/www/epoch-rewrite_log"
RewriteCond %{HTTP_HOST} !^www\.epoch\.co\.il [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.epoch.co.il/$1 [L,R]