htaccess redirect cirtain requests to a different subdomain - apache

I recently upgraded my website, changed the underlying system, and pushed the old content onto a subdomain.
I want the google indexed URLs to the old content to still work, so I added a rewrite rule to the main htaccess that I thought would redirect using looking for the old 2015 posts, but it doesn't appear to be working.
I have added the rule to the top of CraftCMS's default htaccess, which is at the root of my domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/2015(/.*)?$ http://old.keithcod.es/2015$1 [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
I clarify, I'm trying to redirect all traffic from /2015/* to old.keithcod.es/2015/*

Related

http:// redirects to https://www.www with htaccess

I have a CI installation in my root domain. SSL certificate is installed and working properly. When I redirect http:// to https://www it redirects to https://www.www (an extra www), that too on some computers and some browsers as users have reported. However, when I remove 'www' from redirection, its all fine. Seems like www is looping. So far, I've digged my code hundred times, and see no sign of redirection from code (I mean addition of extra www). I'm doing it with htaceess. Any help will be highly appreciated. This is my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Force SSL
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I'm running it on apache with Centos VPS.
Thank you so much!
The HTTP_HOST is the "target host" part of the request, like: www.mydomain.com.
The REQUEST_URI is generally the path which was given in order to access a certain page; for instance, ‘/folder/index.html’.
In your RewriteRule you say to put 'www.' in front of the requested domainname.
You don't want that, when someone asks for http://www.yourdomain.com.
Without the www. in your RewriteRule someone who requests for http://yourdomain.com gets redirected to https://yourdomain.com
When you want to redirect to https and www you need to add conditions. Look into Apache docs on Canonical host and on questions/4083221/how-to-redirect-all-http-requests-to-https for this solution:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTPS_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

url rewrite apache for wildcard domain

I have a rewrite rule for wildcard domains all requests are send to index.php on main domain. This is content for my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.us$
RewriteRule ^/?$ "http\:\/\/domain\.us\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]
And it works like it's supposed to work...perfect!
But recently I had to change the url structure of the whole website, that is fine old urls will simple show 404 though few old urls are important to me and I want to redirect them 301 to new url. For example:
somesubdomain.maindomain.com/oldcontent/
I want to reditect
somesubdomain.maindomain.com/importantdir/oldcontent/
Whatever I tried I ended up with a redirection loop (to many redirects at once).
I have like thousands of urls but only want to 301 redirect few of them.

.htacces - Redirect all requests to a certain directory, except for direct url access

I have a forum and a website which were located in / (root) and /website/. Now, I moved my forum to /forum/ in order to organize the web server better and to be actually able to find something when needed.
What I want to do is somehow redirect requests like
www.mywebsite.com
http://mywebsite.com/
http://www.mywebsite.com/
to /website/ and any request made to my base url but followed by a page (www.mywebsite.com/index.php as an example) to my /forum subfolder.
Is that even possible to do en-mase ? Or can I least redirect if I match a specific file ?
Rule that redirects requests to base url over to my website folder that I have in my .htacces already.
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^/?$ "http\:\/\/www.domain\.com\/website_folder\/" [R=301,L]
Thank you for your help.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^/?$ /website_root/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!(?:website_root|forum)/).+)$ /forum/$1 [L,NC]

Drupal redirect from installation subdirectory without creating a loop

I am working on a Drupal site that has hard coded links in lots of the content (I didn't create the site). Unfortunately that means the site has to be in a subdirectory such as "www.example.com/drupal" or the links will break. I have sucessfully redirected the site root to the subdirectory and hid it using the following code in the site root htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) drupal/$1 [L]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
However if I go to www.example.com/drupal the subdirectory still shows up.This means that www.example.com and www.example.com/drupal have the same content which is bad for SEO. Is there some way to make /drupal redirect to root without causing a loop? I have tried just about every posted in other threads and none of them work.
You need to do the redirect before rewriting the URL. You also need to check that you're actually rewriting to a legitimate resource.
swap the rules around, the redirect should happen first
Add a check to the drupal rule
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/drupal
RewriteRule (.*) drupal/$1 [L]
</IfModule>

Redirecting the user requests with apache mod-rewrite

I have the following rewrite rules to redirect everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
Now I need to modify it to support the following
When user tries to access domain.tld redirect him to www.domain.tld
When user tries to access domain.tld/key/... redirect him (permanent redirect) to key.domain.tld/...
Of course I still need to redirect all traffic to the index.php except for when user tries to access css, javascript and images where I need to serve them directly
domain.tld/css/...
subdomain.domain.tld/css/... apache should directly serve the url (css files)
Similarly I need to add js and images folders
I am awful with Apache ReWrite module so PLEASE help me out here :)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f #if a file, css, js, jpg whatever file
RewriteCond %{REQUEST_FILENAME} !-d #if thats a directory
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ /someprofile.php?username=%2 [QSA,L]
</IfModule>