301 redirect from subdomain to subfolder - .htaccess

I'm trying to set up a 301 redirect from a subdomain to a subfolder, but nothing I do seems to work. The goal is to set up a redirect from the subdomain "learn", to a subdirectory "learn".
learn.site-name.com -> site-name.com/learn
I have not been able to successfully set this up on our (kinsta) staging site. learn.site-name.com just goes straight to site-name.com.
What am I doing wrong??
Here is the .htaccess (site name obscured for client's privacy):
php_value upload_max_filesize 12M
php_value post_max_size 13M
RewriteEngine On
RewriteCond %{HTTP_HOST} ^learn\.XXXX.kinsta\.com$ [NC]
RewriteRule ^(.*)$ http://XXXX.kinsta.com/learn/$1 [L,R=301]

Related

Why is this .htaccess redirect not working?

I have two identical versions of a website, both using different AWS EC2 instances(staging xx.xx.xx.xx and production xxx.xxx.xxx.xxx). I want to create a 301 redirect that redirects all users from the staging site to the production site, UNLESS they are visiting from my IP. I thought this was a relatively simple task, but I have attempting to make this work for the last couple hours with no resolution. I am hoping that someone here will be able to point me in the right direction. Currently, the redirect works for visitors to the homepage http://xx.xx.xx.xx, but it does not work for other pages such as http://xx.xx.xx.xx/page1 or the ec2 public DNS like http://ec2-xx-xx-xx-xx.us-west-1.compute.amazonaws.com/ Here is my entire .htaccess file, the redirect I added is the last one. Any help you could give me would be much appreciated!!
# 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>
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300
# END WordPress
RewriteEngine on
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NC]
RewriteBase /
RewriteCond %{HTTP_HOST} !newsite.com$ [NC]
RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xxx
Thanks for your help, I wanted to post the solution I found in a wordpress forum here:
https://wordpress.org/support/topic/htaccess-help-301-redirects-not-working/
Thanks to #markrh for his answer. The problem was that I needed to move my 301 redirects above the WordPress section at the top. My rules were never getting reached cause the WordPress part catches all of them.

Redirect a page using htaccess in cakePHP

I am trying to redirect one page in cakephp. My guess is that .htaccess is the way to do this.
I'm trying to redirect from /consultation to /pages/consultation2016
I have this:
Redirect /consultation http://thep.ca/pages/consultation2016
<IfModule mod_rewrite.c>
RewriteEngine on
php_value post_max_size 70M
php_value upload_max_fiesize 70M
RewriteBase /app
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Redirect without touching .htaccess:
CakePHP 2 (documentation):
Router::redirect('/consultation','http://thep.ca/pages/consultation2016');
CakePHP 3 (documentation):
$routes->redirect('/consultation','http://thep.ca/pages/consultation2016');

301 htaccess dir redirect to a different subdomain

I would redirect the whole dir "https://sub.domain.ext/dir1" to another location on a different machine "https://sub1.domain.ext/dir1", I'm using in /.htaccess the following rule with no success:
The page simply doesnt't redirect.
Rewriterule ^dir1/(.*) https://sub1.domain.ext/dir1/$1 [R=301,L]
# this is the whole ht access
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Morfeus
RewriteRule ^.*$ - [F]
</IfModule>
# supress php errors
php_value display_errors off
# force https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://sub.domain.ext/$1 [R,L]
any tips?
Scenario: ubuntu+apache
others htaccess rules worsk fine
thx
Deleting the htaccess file in the dir1/, the problem was solved.

.htaccess config for www to non-www in Symfony2 app on Openshift

I have a Symfony app deployed on openshift (it could be any PAAS server)
My domain uses CNAME flattening pointing like this:
example.com -> example-user.rhcloud.com
www -> example.com
My app in Openshift have these aliases:
example-user.rhcloud.com
example.com
www.example.com
My .htacces is the original from Symfony2 located in /web
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Everything works fine but I want to point all requests from www.example.com pointing to example.com
I tried adding these lines to my .htacces:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
but when I try opening www.example.com my browser says there is a redirect loop. (example.com keeps working well)
How can I change the .htaccess file in order to point all www access pointing to the non-www domain?
Ok, I found a solution that works very well.
So I didn't have to change anything in the .htaccess file and put a redirect direct from the DNS manager which mine is cloudfare as it uses CNAME flattening.
I have two aliases for my app (example-user.rhcloud.com):
example.com
www.example.com
My domain is from godaddy, but with DNS to Cloudfare
In Cloudfare I added my site, and in DNS Settings I put two records:
CNAME - example.com - is an alias of example-user.rhcloud.com - TTL Automatic
CNAME - www - is an alias of example.com
Then I saved changes and opened Page Rules of the same site
I created a new page rule as follows:
URL pattern: www.example.com
Forwarding ON
Destination URL: http://example.com
Forwarding Type: 302
And that's it, now the main www route point to the one without www
Not completely related, but if your site is dependent on traffic from search engines, it is good SEO practice to use a 301 redirect when the redirect is permanent. Read more about it here on MOZ.com

Drupal URL change, rewrite goes to static

Setup on media temple, second domain name, same server.
Website duplicated from domainA.com, to domainB.com, under the vhosts. Changed drupal's settings.php to reflect the new domain. When checking under the admin settings, the default front page is listed under the domainB.com URL. So that checks out okay.
Navigate to domainB.com and it resolves as domainA.com's name.
Both domainA & domainB are loading from domainB's folder. This assumption is made because I only changed settings.php in domainB, not domainA, and they are both displaying the data for domainB.
The issue is the address bar is displaying domainA's name.
domainB's htaccess file has:
# enforce https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domainB.com/$1 [R,L]
# redirect www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domainB\.com$ [NC]
RewriteRule ^(.*)$ https://domainB.com/$1 [L,R=301]
# drupal subdirectory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/$1 [L]
RewriteRule ^$ drupal/ [L]
# disallow indexes
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
php_flag log_errors on
php_value error_reporting 32767
php_value error_log "/var/www/vhosts/domainB.com/logs/php_error.log"
Goals:
1) domainB is identical in operation to domainA, except for domainB's name displaying in the navigation bar and internal links.
2) domainA points to a static page.

Resources