.htaccess Redirect/Rewrite Issue - apache

I am having a hard time figuring out how I can do this.
I want all requests to www.mydomain.com/manager to redirect to m.mydomain.com
I am trying:
# Friendly URLs for the manager
RewriteCond %{HTTP_HOST} ^(www.)?cloudcms.co$
RewriteRule ^manager/?$ http://m.cloudcms.co/ [NC,L,R]
RewriteCond %{HTTP_HOST} ^(m.|manager.)?cloudcms.co$
RewriteRule ^(/)?$ manager/ [L,QSA,NC]
However, the only real one that is properly rewriting is the second condition.
I have also tried the following
# Friendly URLs for the manager
RewriteCond %{HTTP_HOST} ^(www.)?cloudcms.co$
Redirect 301 /manager http://manager.cloudcms.co
RewriteCond %{HTTP_HOST} ^(m.|manager.)?cloudcms.co$
RewriteRule ^(/)?$ manager/ [L,QSA,NC]
But it ignores the second rewrite

As per my comment above:
Is there another .htaccess in /manager folder or any other in present .htaccess`
I suspect OP has another .htaccess somewhere inside your DOCUMENT_ROOT. Try removing that and this simple redirect will start working.
OP asked:
How would I be able to do this for any domain that could come in and
try hitting the /manager folder?
Just change your first redirect rule to:
# if not already on mobile domain
RewriteCond %{HTTP_HOST} !^m\. [NC
RewriteRule ^manager/?$ http://m.cloudcms.co/ [NC,L,R=301]

You probably don't want to P flag, which is reverse proxying for you. Try replacing that with an R:
RewriteRule ^manager/?$ http://m.mydomain.com/ [NC,L,R]

Related

.htaccess issue with using a -org secondary domain vs using a .com secondary domain

I have successfully forwarded www.atlantalawvideo.legalvideoconsulting.com to www.atlantalawvideo.com for SEO purposes.
I'm using this code in my .htaccess file in my /atlantalawvideo/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.atlantalawvideo.com
RewriteRule (.*) http://www.atlantalawvideo.com/$1 [R=301,L]
I'm having trouble forwarding www.legalvideographer-org.legalvideoconsulting.com to www.legalvideographer.org
I'm using this code in my .htaccess file in my /legalvideographer-org/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.legalvideographer.org
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
Not sure what the problem is http://www.legalvideographer-org.legalvideoconsulting.com forward goes nowhere.
The second rewrite is not working because you are not meeting the condition of the %{HTTP_HOST}. You're not using the full host name to check. legalvideographer.org appears to be a subdomain oflegalvideoconsulting.com. See if this works for you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.legalvideographer-org\.legalvideoconsulting\.com [NC]
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
That should work if that is your domain name.
You should also be able to do a simple redirect instead of the rewrite rule
redirct 301 / http://www.legalvideographer.org

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.

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

htaccess for redirect to SSL

For the past few hours (days) I have been having some trouble redirecting a
page to SSL.
My setup is as follows: I have the following .htaccess for an e-commerce site
on Apache 2.2.16 on Debian (all required mods enabled)
RewriteEngine On
RewriteBase /shop
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
all requests are passed to index.php which acts as my controller and includes
other .php files as necessary.
I now want to use HTTPS for the checkout process which is a php script
cleverly called checkout.php
I thought it would be as easy as changing my .htaccess to:
RewriteEngine On
RewriteBase /shop
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_URI} checkout\.php
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
so that checkout.php is not processed by index.php.
Apparently it is not that simple. I could probably do it by using a hardcoded
https link to checkout but I would prefer to do it with mod_rewrite.
If anyone can share some insight into
this it would be really appreciated.
Thanks in advance
There are a few problems. First, the pattern in your first RewriteRule
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
is written incorrectly. $1 isn't meaningful there (it's a capture result, but no capture has happened yet), and also the query string (part of the request after the ?) isn't part of what's matched, as the RewriteRule documentation says.
Second, I think you meant to use REQUEST_URI instead of SERVER_URI.
So I think you want something like this:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/checkout\.php
RewriteRule .* https://localhost/shop/checkout.php [L,R]
RewriteCond %{REQUEST_URI} !^/(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ /index.php?/$1 [L]
A few notes:
You don't need to match or add back in the query string in the first RewriteRule; mod_rewrite will automatically add it back in.
It's conventional to test RewriteCond %{HTTPS} off instead of
RewriteCond %{SERVER_PORT} !443, as #Jon Lin suggests.
You may want to add the QSA flag in your second RewriteRule.

How do I use mod_rewrite to alter which file is fetched based on the URL's sub-domain?

So say I have a URL being requested:
sub.domain/file.bin
and I wanted this file to be fetched:
domain/hosted/sub/file.bin
How would I structure the rewrite directives? I've been working on this for days, and it just gives me a headache...
The sub-domains are numerous and are added and removed often, so I'm trying to find a solution that works for all of them.
In sub.domain's httpd config:
RewriteCond %{HTTP_HOST} ([^\.]+).domain
RewriteRule ^/file.bin$ http://domain/hosted/%1/file.bin
Assuming the home directory for www.domain.com is domain/ and that - given any subdomain sub.domain.com - you want the files for the home directories for that sub-domain to be in domain/hosted/sub/
Try something like this:
RewriteEngine on
// If the host is just mydomain.com, do nothing more
// this is to prevent some recursion problems I've read of...
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ %1%{REQUEST_URI} [QSA]
// Then prefix with 'hosted'
RewriteRule ^(.*)$ hosted/$1 [QSA,L]
You may also need a wildcard entry in your DNS or something... but I will admit DNS and htaccess mod rewrite are some of my weaker points. See also http://www.webmasterworld.com/forum92/138.htm
Try this rule:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule !^hosted/ hosted/%1%{REQUEST_URI} [L]