Apache Redirect (using .htaccess) - apache

I'm sort of a novice at using mod_rewrite in .htaccess, please forgive me. I've searched far and wide for an answer but perhaps I'm not looking in the right places, or perhaps .htaccess isn't even the right place to do this kind of thing.
Let's say I own two domains: joebloggs.com and bloggs.com.
When you put joebloggs.com into your browser I'd like it to redirect to https://www.bloggs.com/joe.
However, if you go to joebloggs.com/foobar, I'd like to redirect to https://www.bloggs.com/foo/bar.
note: I know this seems counterproductive, going from a shorter URL to a longer one, but the problem is that marketing materials were already disseminated with the URL joebloggs.com/foobar... :-/
The way I've tried to structure this in the .htaccess file hasn't worked thus far.
<IfModule mod_rewrite.c>
RewriteEngine on
#
# redirect joebloggs.com/foobar to https://www.bloggs.com/foo/bar
RewriteCond %{HTTP_HOST} ^joebloggs\.com\/foobar [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com\/foobar [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs\.com\/foo\/bar [L,R=301,NC]
#
# redirect joebloggs.com to https://www.bloggs.com/joe
RewriteCond %{HTTP_HOST} ^joebloggs\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com$ [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs.com\/joe [L,R=301,NC]
</IfModule>
When I put joebloggs.com into a browser, it redirects correctly to https://www.bloggs.com/joe - but when I try to go to joebloggs.com/foobar, the URL remains intact and thus goes to a 404 page (because it doesn't exist).
What am I doing wrong? Am I not doing this the optimal way, or not using .htaccess/mod_rewrite as it was intended?
Sorry for being long-winded, I hope I gave enough information. Thanks for any help and time spent on this!

The first set of rules is entirely useless. Update the second set to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^/?$ https://www.bloggs.com/foo [R=301,L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^foo(.*)$ https://www.bloggs.com/foo/$1 [R=301,L,NC,QSA]

Related

htaccess not updating on the server?

I'm at a bit of a loss. I'm working on a website so I have created a temporary construction.html page that I want to redirect all IPs to other than my own. Here is what my .htaccess looks like:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R,L]
ErrorDocument 404 http://www.example.org/construction.html
Options +FollowSymlinks
RewriteCond %{REMOTE_ADDR} !^12\.34\.567\.891
RewriteCond %{REQUEST_URI} !/construction.html$ [NC]
RewriteRule .* /construction.html [R=302,L]
What's strange is that it seemed to work initially, but my IP still gets rejected most of the time, but I haven't found a pattern. Sometimes I can access the root index.html if I explicitly navigate to it.
What's even stranger is that if I comment out those last four lines, I still get redirected to the construction.html page.
I've made sure to clear the cache of the browser, and that doesn't seem to do anything. Perhaps it is a syntax thing? My understanding is that the changes should be instantaneous.
Based on your shown attempts, could you please try following. Please make sure that your index.html, consutuctions.html and .htaccess rules files are present in root folder. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymlinks
RewriteEngine On
##Block access to index.html here..
RewriteRule ^index\.html/?$ - [NC,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)/?$ https://www.example.org/$1 [NE,R=302,L]
RewriteCond %{REMOTE_ADDR} !^12\.34\.567\.891
RewriteCond %{REQUEST_URI} !/construction\.html/?$ [NC]
RewriteRule ^ /construction.html [R=302,L]
ErrorDocument 404 http://www.example.org/construction.html

How to redirect properly

I´m trying to redirect with rewrite rules in Apache .htaccess.
I want to redirect 360invest.pl to 360investment.pl.
But I want one more address on the server which I do NOT want to be redirected. If somebody tries 360invest.pl/bezrzecze it should not redirect to 360investment.pl.
I used this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^360invest.pl/bezrzecze$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP_HOST} ^360invest.pl$ [NC]
RewriteRule ^ http://www.360investment.pl%{REQUEST_URI} [L,R=301]
This works only if you try www.360invest.pl/bezrzecze. It doesn't work if you try http://360invest.pl/bezrzecze.
In addition if you click any link on the website (www.360invest.pl/bezrzecze) it will redirect you to www.360investment.pl - it should not.
I'm not sure what I'm missing here. Thank you for your help.
What you are doing is really messy. The first condition does not make sense, because "360invest.pl/bezrzecze" is not a domain name. It is a domain name and a path. The following conditions with [OR] flags make no sense to me.
Think about what you want to accomplish. Start with the general case, then list your exceptions:
If someone goes to 360invest.pl you want to redirect all urls to 360investment.pl
unless someone goes to the path bezrzecze
The first part is the rule. The second part is a condition that has to be met
RewriteCond %{REQUEST_URI} !^/bezrzecze
RewriteRule ^ http://www.360investment.pl%{REQUEST_URI} [L,R]

Proper htaccess 301 to prevent duplicate hostname issues?

I want to make sure there are no duplicate hostname issues and Google Analytics has suggested there are.
So, I want to make sure my home page is always www.example.com and not example.com or I suppose http://www.example.com
How is this done safely?
I found one example, but as I am not a guru at this sort of thing, figured I needed a second opinion or two:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
The more explanation the better...
You regex in RewriteCond looks suspect since it makes whole hostname optional. You can have this rule instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]

How to implement ssl on everything except a specific and/or a few specific files/folders using .htaccess

I am implementing SSL on everything but would like to exclude a specific page or specific set of pages from using SSL. How can I do this. At the moment I am using the simple syntax:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
If you could could you explain the steps you are taking in order to do this please so that I can actually understand why and what you have done and I will learn something from this?
Thanks for the help,
John
You can use it like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/(excluded1|excluded2)/ [NC]
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
RewriteCond %{REQUEST_URI} ! is for excluding listed paths in https redirection.

mod_rewrite: remove www and redirect into folder if not already in URL

First time here, and I'm a bit of a mod_rewrite and regex noob, so please let me know if I've missed anything. I've searched here and elsewhere all day but not found exactly what I need.
I'm looking for a set of RewriteConds and RewriteRules that will accomplish the following:
remove the www, if present in URL
redirect into a folder, if not already present in URL
preserve the rest of the URL
I have a web app installed in a specific subfolder (we'll call it /webapp) which is configured to require no www on the url. It presents a stupid annoying message to the user if they include the www. I can dig into the app and reprogram that, but I'd like to handle that for the users through .htaccess and mod_rewrite, and simultaneously dump them into the folder, if they forget to type it, doing all of this with a 301 redirect.
For example, I'd like any of the following requests
http://www.mydomain.org/webapp/anything
http://www.mydomain.org/anything
http://mydomain.org/anything
To be redirected to
http://mydomain.org/webapp/anything
And obviously if a "correct" URL (one starting with http://mydomain.org/webapp/) is requested, it's not rewritten at all.
My best guess so far is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=302]
RewriteCond %{REQUEST_URI} !^/webapp.*$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/webapp/$1 [R=302]
This seemed to work according to http://htaccess.madewithlove.be/ but in practice, not so much.
Thanks in advance.
try:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$
RewriteRule ^ mydomain.org/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteRule ^/(.*) mydomain.org/webapp/$1 [L,R=301]
Found the answer myself here: BowlerHat
Looks like this:
# First just remove the www
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [L,R=301]
# Now redirect into the folder
RewriteCond %{REQUEST_URI} !webapp/ [NC] # if the provided URI does not start with /webapp,
RewriteRule (.*) http://mydomain.org/webapp/ [L,R=301] # redirect user to /webapp/ root
I decided that if users try to visit mydomain.org/somethingsomething, just to send them to the root of the webapp, rather than /webapp/somethingsomething.